简体   繁体   English

Android应用程序崩溃onCreate

[英]Android application crashing onCreate

when I distinguish between two intents using their getAction() value, my application crashes. 当我使用它们的getAction()值区分两个意图时,我的应用程序崩溃了。 When I remove this code, it does not, so I figure it has something to do with this block of code: 当我删除此代码时,它不会删除,因此我认为它与以下代码块有关:

Intent intent = getIntent();
String action = intent.getAction()

if(action.equals(ViewFavorites.SOURCE) {
    //Do something
} else if(action.equals(AppSettings.SOURCE) {
    //Do something
}

This code is inside my MainActivities onCreate method. 这段代码在我的MainActivities onCreate方法中。 In the the ViewFavorites and AppSettings the action is set to the SOURCE static field. 在ViewFavorites和AppSettings中,将操作设置为SOURCE静态字段。 But when this code runs... my application crashes... heres the two other class file code blocks that deal with calling the back the MainActivity which is (WeatherDisplay... 但是当这段代码运行时...我的应用程序崩溃了...这是另外两个处理回调用MainActivity的类文件代码块(WeatherDisplay ...

ViewFavorites: ViewFavorites:

Intent intentWeatherDisplay = new Intent(this, WeatherDisplay.class);
intentWeatherDisplay.setAction(SOURCE);
startActivity(intentWeatherDisplay);

AppSettings: 的AppSettings:

Same thing as code above ^

It looks action can be null . 看起来action可以为null Use reversed construct to avoid NPE. 使用反向构造以避免NPE。

if (ViewFavorites.SOURCE.equals(action) {
    //Do something
} else if(AppSettings.SOURCE.equals(action) {
    //Do something
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM