简体   繁体   中英

I'm seeing an `android.content.ActivityNotFoundException` when trying to open a web browser from my app

The following code is giving me an android.content.ActivityNotFoundException every so often.

I use identical code in a few of my activities to direct the user to additional information on a web site and it seems to work most of the time without problems.

private void openWebPage(String url_to_view) {
    Intent open_web_page_intent = new Intent();

    if (url_to_view != null && !url_to_view.equals("")) {
        open_web_page_intent.setAction(Intent.ACTION_VIEW);
        try {
            open_web_page_intent.setData(Uri.parse(url_to_view));
            startActivity(open_web_page_intent);
        } catch (ParseException e) {
            Log.e(LOGTAG, "URI Parse Exception in Open Web Page");
        }
    }
}

Is there a way to safeguard against this error?

I can't figure out from the stack trace if the problem is with the calling Acttivity or with the new Activity that I am starting? (Full stack trace below)

If the problem is with the new activity, would a try/catch around the StartActivity statement be a good safeguard? (The crash is infrequent and I can't reproduce it on my test devices or AVDs).

Note also that the stack trace refers to StartActivityForResult which I am not doing in this instance, I am just starting the new activity normally.

android.content.ActivityNotFoundException: 
  at android.app.Instrumentation.checkStartActivityResult (Instrumentation.java:2071)
  at android.app.Instrumentation.execStartActivity (Instrumentation.java:1717)
  at android.app.Activity.startActivityForResult (Activity.java:5252)
  at androidx.fragment.app.FragmentActivity.startActivityForResult (FragmentActivity.java:6)
  at android.app.Activity.startActivityForResult (Activity.java:5203)
  at androidx.fragment.app.FragmentActivity.startActivityForResult (FragmentActivity.java:6)
  at android.app.Activity.startActivity (Activity.java:5581)
  at android.app.Activity.startActivity (Activity.java:5549)
  at com.nooriginalthought.amalfi.FirstRun.openWebPage (FirstRun.java:27)
  at com.nooriginalthought.amalfi.FirstRun.lambda$initialize$4 (FirstRun.java:1)
  at com.nooriginalthought.amalfi.FirstRun.lambda$initialize$4$FirstRun (FirstRun.java:1)
  at com.nooriginalthought.amalfi.-$$Lambda$FirstRun$iHROIfEVj1qDNupIO6vj8JGEmlM.onClick (-.java:1)
  at android.view.View.performClick (View.java:7869)
  at android.widget.TextView.performClick (TextView.java:14958)
  at android.view.View.performClickInternal (View.java:7838)
  at android.view.View.access$3600 (View.java:886)
  at android.view.View$PerformClick.run (View.java:29362)
  at android.os.Handler.handleCallback (Handler.java:883)
  at android.os.Handler.dispatchMessage (Handler.java:100)
  at android.os.Looper.loop (Looper.java:237)
  at android.app.ActivityThread.main (ActivityThread.java:8107)
  at java.lang.reflect.Method.invoke (Method.java)
  at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run (RuntimeInit.java:496)
  at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1100)

Is there a way to safeguard against this error?

Wrap any startActivity() or startActivityForResult() call that uses an implicit Intent in a try / catch block, where you catch ActivityNotFoundException . Bear in mind that there is no requirement for the user of your app to have access to a Web browser (eg, child with a restricted account).

I can't figure out from the stack trace if the problem is with the calling Acttivity or with the new Activity that I am starting?

It is from com.nooriginalthought.amalfi.FirstRun.openWebPage (FirstRun.java:27) . We do not have your full file and cannot tell you which line that is.

Note also that the stack trace refers to StartActivityForResult which I am not doing in this instance, I am just starting the new activity normally.

Your code is calling startActivity() , as you can see in the at android.app.Activity.startActivity (Activity.java:5549) line of the stack trace. Eventually, that calls startActivityForResult() internally.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM