安卓:活动未发现异常某些设备上,试图打开本地的HTML时 - 在浏览器中的文件 [英] Android: Activity not found exception on some devices, when trying to open local HTML - file in browser

查看:554
本文介绍了安卓:活动未发现异常某些设备上,试图打开本地的HTML时 - 在浏览器中的文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到一个奇怪的错误,当我尝试打开一个本地的HTML - 在Android浏览器中的文件。出现的错误是一个活动未发现异常:

  android.content.ActivityNotFoundException:无法找到明确的活动类{com.android.browser / com.android.browser.BrowserActivity};有你宣布你的Andr​​oidManifest.xml这个活动?
 

什么是真正奇怪,我北京时间,这个错误不会在所有设备上发生的事实。它只出现在下列设备:

  • 在三星Galaxy Nexus的我(4.1.1)
  • 三星GALAXY Nexus II(4.1.1)
  • 索尼Tablet S的(4.0.3)

我对以下设备成功测试了我的code:

  • 在三星Galaxy S I(4.1.1)
  • 在三星Galaxy S III
  • 在HTC One X的
  • HTC Desire S的
  • 在三星Galaxy S加(2.3.3)
  • 三星Galaxy Tab 10.1N
  • AVDS与Android版本从2.3.3到4.1.0

最后,这是code我用打开我的HTML - 文件了Android - 浏览器。我测试了这code数排列,留下了一些线路,但它们都具有相同的效果。当我设置的组件或类我得到了上面的异常。

 开放的URI = Uri.parse(文件路径);
意图browserIntent =新的意图(Intent.ACTION_VIEW);
browserIntent.setComponent(新单元名(com.android.browser,com.android.browser.BrowserActivity));
browserIntent.setDataAndType(URI,text / html的);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
 

我也尝试

  browserIntent.setClassName(com.android.browser,com.android.browser.BrowserActivity);
 

而不是

  browserIntent.setComponent(新的组件名(com.android.browser,com.android.browser.BrowserActivity));
 

但具有同样的影响......

有一个人一个想法,为什么发生这种情况,我失去了一些东西?我搜索了这个日子,但找不到任何解决我的问题...

感谢很多提前, 欢呼声

解决方案

可能,因为可能会存在一样,没有任何活动 com.android.browser.BrowserActivity 在这些设备,它的取决于设备制造商他们是如何实现本机的浏览器应用程序(活动名称和包的名称)。

因此​​,可行的办法是,

使用 PackageManger 意图您可以检查特定意图类一样,意图。 CATEGORY_BROWSABLE 适用于任何应用程序(如果有),然后设置应用程序组件名。

或者,不指定组件名称一样,

  browserIntent.setComponent(新的组件名(com.android.browser,com.android.browser.BrowserActivity));
 

让用户有选择,这活动会打开这个​​页面,

因此​​,只要code是,

 开放的URI = Uri.parse(文件路径);
意图browserIntent =新的意图(Intent.ACTION_VIEW);
browserIntent.setDataAndType(URI,text / html的);
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);
 

I'm encountering a strange error when I try to open a local HTML - file in the android browser. The error that occurs is an activity not found exception:

android.content.ActivityNotFoundException: Unable to find explicit activity class {com.android.browser/com.android.browser.BrowserActivity}; have you declared this activity in your AndroidManifest.xml?

What's really strange to me ist the fact that this error does not occur on all devices. It only comes up on the following devices:

  • Samsung Galaxy Nexus I (4.1.1)
  • Samsung Galaxy Nexus II (4.1.1)
  • Sony Tablet S (4.0.3)

I have tested my Code successfully on the following devices:

  • Samsung Galaxy S I (4.1.1)
  • Samsung Galaxy S III
  • HTC One X
  • HTC Desire S
  • Samsung Galaxy S Plus (2.3.3)
  • Samsung Galaxy Tab 10.1N
  • AVDs with Android Versions ranging from 2.3.3 to 4.1.0

Finally this is the Code I'm using to open my HTML - file with the Android - browser. I have tested several permutations of this code, leaving out some lines, but they do all have the same effect. As soon as I set component or class I getting the above exception.

Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);

I have also tried

browserIntent.setClassName("com.android.browser", "com.android.browser.BrowserActivity");

instead of

browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));

But with the same effect...

Has someone an idea why this is happening, am I missing something? I have searched for days on this but couldn't find anything that solved my problem...

Thanks a lot in advance, cheers

解决方案

Possible because may be there is no any activity like com.android.browser.BrowserActivity in those devices, Its depends on device manufacturer How they implement the native Browser Application (Activity Name and Package name).

So the possible solution is,

Using PackageManger and Intent you can check for specific intent category like, Intent.CATEGORY_BROWSABLE is available for any application if available then set that application to ComponentName.

Or, You don't specify component name, like,

browserIntent.setComponent(new ComponentName("com.android.browser", "com.android.browser.BrowserActivity"));

let user have to choose, which Activity will open this page,

So just code is,

Uri uri = Uri.parse(filePath);
Intent browserIntent = new Intent(Intent.ACTION_VIEW);
browserIntent.setDataAndType(uri, "text/html");
browserIntent.addCategory(Intent.CATEGORY_BROWSABLE);
context.startActivity(browserIntent);

这篇关于安卓:活动未发现异常某些设备上,试图打开本地的HTML时 - 在浏览器中的文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆