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

问题描述

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

那么,如何防范呢?

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

如果我读this正确的实例的运行时异常(NPE)被抛出。我喜欢使用更具体的东西,虽然如 ActivityNotFoundException - 但它足以

解决方案

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

这应该是。

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

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

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 ?

解决方案

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

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.

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

09-01 22:03