我在活动onCreate()中初始化列表,如下所示:
private List<MyItem> filtered;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
filtered = new ArrayList<>();
// more things
}当我尝试使用来自onNewIntent的过滤项时,有时我会得到一个空指针异常。
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
filtered.clear();
}怎么可能呢?
编辑:我的活动的启动模式是SingleTask
编辑2:
我无法发送更多有用的日志,因为这种崩溃正在发生。只有我能得到一些布料原木。
谢谢你的帮助,但我不能粘贴整个代码的隐私。
我想我对SingleTask-OnCreate-OnNewIntent的使用有问题。简单地说,我试图从通知中打开我的应用程序,并使用一个参数来决定用户导航到活动时将打开哪个片段。
您有关于这个包含SingleTask-OnCreate-OnNewIntent实现的示例吗?
感谢大家的帮助。
发布于 2017-03-20 21:52:28
Open系统的团队也遇到了同样的问题:
https://github.com/WhisperSystems/Signal-Android/issues/2971
他们认为这是因为安卓框架在onCreate()之后不久就调用了finish(),即使你在onCreate()中调用了finish()。这最终导致null对象在onNewIntent()中使用,然后在NullPointerException中使用,因为这些对象还没有在通常的onCreate()中设置。这似乎是一种罕见的场景,或者是onCreate()和onNewIntent()之间的竞争条件。
他们似乎通过在onNewIntent()中检查onNewIntent()中的onNewIntent()以防止onNewIntent()继续运行来修复它:
@Override
protected void onNewIntent(Intent intent) {
Log.w(TAG, "onNewIntent()");
if (isFinishing()) {
Log.w(TAG, "Activity is finishing...");
return;
}
...
}来源1:https://github.com/SilenceIM/Silence/commit/c0acae1124c0c066fd961d21d3ec9b989574125d
更新:2017年11月10日:当升级应用程序/build.gradle中的构建工具时,这个问题似乎很少发生:
buildToolsVersion '25.0.2'在较旧的构建工具(如buildToolsVersion '23.0.3' )中,这种情况似乎更多发生,但修复仍然是必要的,因为它仍然发生在很少的情况下。
发布于 2015-09-24 09:20:52
记住生命周期,也许您是从没有调用onCreate()的某个点返回到您的活动的(例如,如果您在清单中设置了launchMode=singleTask或其他东西)。也许可以在ArrayList ()中初始化/重新填充onResume(),也可以检查它是否为null,在恢复()返回到活动时肯定会调用恢复()。
发布于 2015-09-24 09:19:12
如果在NullPointerException方法中获得onNewIntent(),那么filtered对象可能为null。
试试这边
@Override
protected void onNewIntent(Intent intent) {
//Check for null value and then proceed.
if(filtered != null)
filtered.clear();
} https://stackoverflow.com/questions/32757542
复制相似问题