问题描述
大多数活动炬力(用于发射各种设置活动)在设置班都配备了警告:
那么,如何防范呢?
尝试{
最终意图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设置的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!