防范"匹配的活动可能不存在"在Android设置 [英] Safeguard against "a matching Activity may not exist" in android settings

查看:206
本文介绍了防范"匹配的活动可能不存在"在Android设置的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大多数活动炬力(用于发射各种设置活动)在设置班都配备了警告:

  

在一些情况下,匹配的活动可能不存在,所以确保你防范这

那么,如何防范呢?

 尝试{
    最终意图I =新的意向书(设置ACTION_WIRELESS_SETTINGS); // 说
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); //不知道是否需要的
    startActivity(ⅰ);
}赶上(例外五){//我应该赶在这里
    //我不想捕获Throwable,但要这样呢?
}
 

如果我读<一href="http://stackoverflow.com/questions/12474777/android-settings-action-device-info-settings-crashes-on-android-4-1-1">this正确的实例的运行时异常(NPE)被抛出。我喜欢使用更具体的东西,虽然如 ActivityNotFoundException - 但它足以

解决方案
  

如果我读这正确,例如运行时异常(NPE)被抛出

没有,这是一些其他的问题。该意图清楚地工作,因为飞机坠毁是由设置到来本身,而不是调用该应用程序 startActivity()

  

我喜欢使用更具体的东西,虽然像ActivityNotFoundException - 但它足以

这应该是。

如果您担心的是,还是宁愿要积极主动,而不是仅仅调用 startActivity(),第一次使用 PackageManager resolveActivity()。如果返回,没有活动相匹配的意图,你应该尝试其他的东西。

The majority of the Activity Actions (used to launch various Settings activities) in the Settings class come with a warning :

In some cases, a matching Activity may not exist, so ensure you safeguard against this.

So how do I safeguard against this ?

try {
    final Intent i = new Intent(Settings. ACTION_WIRELESS_SETTINGS); // say
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); // not sure if needed
    startActivity(i);
} catch (Exception e) { // what should I catch here 
    // I would hate to catch Throwable, but should I ?
}

If I read this correctly for instance a runtime exception (NPE) is thrown. I would love to use something more specific though like ActivityNotFoundException - but is it enough ?

解决方案

If I read this correctly for instance a runtime exception (NPE) is thrown

No, that's some other problem. The Intent clearly worked, as the crash is coming from Settings itself, not the app that called startActivity().

I would love to use something more specific though like ActivityNotFoundException - but is it enough ?

It should be.

If you are concerned about that, or would rather be proactive, rather than just calling startActivity(), first use PackageManager and resolveActivity(). If that returns null, there is no activity that matches the Intent, and you should try something else.

这篇关于防范&QUOT;匹配的活动可能不存在&QUOT;在Android设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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