尝试从我的应用程序打开网络浏览器时,我看到了“android.content.ActivityNotFoundException”
I'm seeing an `android.content.ActivityNotFoundException` when trying to open a web browser from my app
下面的代码经常给我一个 android.content.ActivityNotFoundException
。
我在我的一些活动中使用了相同的代码来引导用户访问网站上的其他信息,并且它似乎在大多数时间都可以正常工作。
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");
}
}
}
有没有办法防止这个错误?
我无法从堆栈跟踪中找出问题出在调用 Activity 还是我正在启动的新 Activity 上? (下方的完整堆栈跟踪)
如果问题出在新的 activity 上,try/catch
围绕 StartActivity
语句是否是一个很好的保护措施? (崩溃很少见,我无法在我的测试设备或 AVD 上重现)。
另请注意,堆栈跟踪指的是 StartActivityForResult
,我在这种情况下没有这样做,我只是正常启动新的 activity。
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 (FirstRun.java:1)
at com.nooriginalthought.amalfi.FirstRun.lambda$initialize$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.access00 (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?
将任何使用隐式 Intent
的 startActivity()
或 startActivityForResult()
调用包装在 try
/catch
块中,您会在其中捕获 ActivityNotFoundException
.请记住,您的应用程序的用户无需访问 Web 浏览器(例如,child 使用受限帐户)。
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?
来自com.nooriginalthought.amalfi.FirstRun.openWebPage (FirstRun.java:27)
。我们没有您的完整文件,无法告诉您那是哪一行。
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.
您的代码正在调用 startActivity()
,正如您在堆栈跟踪的 at android.app.Activity.startActivity (Activity.java:5549)
行中看到的那样。最终,它会在内部调用 startActivityForResult()
。
下面的代码经常给我一个 android.content.ActivityNotFoundException
。
我在我的一些活动中使用了相同的代码来引导用户访问网站上的其他信息,并且它似乎在大多数时间都可以正常工作。
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");
}
}
}
有没有办法防止这个错误?
我无法从堆栈跟踪中找出问题出在调用 Activity 还是我正在启动的新 Activity 上? (下方的完整堆栈跟踪)
如果问题出在新的 activity 上,try/catch
围绕 StartActivity
语句是否是一个很好的保护措施? (崩溃很少见,我无法在我的测试设备或 AVD 上重现)。
另请注意,堆栈跟踪指的是 StartActivityForResult
,我在这种情况下没有这样做,我只是正常启动新的 activity。
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 (FirstRun.java:1)
at com.nooriginalthought.amalfi.FirstRun.lambda$initialize$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.access00 (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?
将任何使用隐式 Intent
的 startActivity()
或 startActivityForResult()
调用包装在 try
/catch
块中,您会在其中捕获 ActivityNotFoundException
.请记住,您的应用程序的用户无需访问 Web 浏览器(例如,child 使用受限帐户)。
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?
来自com.nooriginalthought.amalfi.FirstRun.openWebPage (FirstRun.java:27)
。我们没有您的完整文件,无法告诉您那是哪一行。
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.
您的代码正在调用 startActivity()
,正如您在堆栈跟踪的 at android.app.Activity.startActivity (Activity.java:5549)
行中看到的那样。最终,它会在内部调用 startActivityForResult()
。