如何修复"android.content.ActivityNotFoundException"android-studio 2.3.3 [英] How to fix "android.content.ActivityNotFoundException" android-studio 2.3.3
问题描述
我有一个小问题,当我单击应用程序中的某个按钮时,该应用程序每次都会完全崩溃,而不会失败.
I have a small problem where when I click a certain button within my app, the app completely crashes each time without fail.
我正在使用android studio 2.3.3,并且该应用是条形码扫描仪,这是我收到的错误消息:
android.content.ActivityNotFoundException:未找到要处理的活动意图{act = android.intent.action.VIEW dat = 5010029217902}
android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=5010029217902 }
这是导致错误的代码部分:
Here is the section of code that is causing the error:
}
});
builder.setNeutralButton("Visit", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
startActivity(browserIntent);
}
});
builder.setMessage(result.getText());
AlertDialog alert1 = builder.create();
alert1.show();
推荐答案
扫描条形码的结果通常不是有效的URL.它通常只是一个带有多个数字的字符串.它没有 schema 或 protocol ,因此(很有可能)未定义它是什么类型的资源定位符".Android的ACTION_VIEW意图主要是使用此信息来决定启动哪个应用程序/活动来打开此URL".由于缺少重要信息,Android无法打开它.
The result when you scan a barcode is often not a valid URL. It is often just a string with several digits. It has no schema or protocol, so it is (very possibly) not defined "what type of resource locator it is". Android's ACTION_VIEW intent is mostly use this information to decide "start which app/activity to open this URL". With a lack of the important information, Android has no idea to open it.
您可以指定某些情况来处理结果.例如,如果结果以"http://"或"https://"开头,则直接使用您的代码进行处理,但是如果它只是一个数字字符串,则直接显示它,或者将其附加在某个字符串之前它用于 Uri.parse
,例如" https://google.com/search?q = ",以根据需要搜索此条形码的值,或要对该13位结果进行其他操作.
You may specify some cases for handling the result. For example, if the result begins with "http://" or "https://", directly use your code to handle, but if it is just a string of numbers, display it directly, or append it after some string before it is used for Uri.parse
, for example "https://google.com/search?q=", to search this barcode's value as you may wanted, or other things you want to do with that 13 digit result.
例如,(下面的用手机编写的代码尚未经过测试,只是说明了这一点):
For example, (the code below written in mobile hasn't been tested, just show the idea):
@Override
public void onClick(DialogInterface dialog, int which) {
Intent browserIntent;
if (myResult.startsWith("http://") || myResult.startsWith("https://"))
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse(myResult));
else
browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("https://google.com/search?q=" + myResult));
startActivity(browserIntent);
}
这篇关于如何修复"android.content.ActivityNotFoundException"android-studio 2.3.3的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!