简体   繁体   中英

Android application crashing onCreate

when I distinguish between two intents using their getAction() value, my application crashes. 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. In the the ViewFavorites and AppSettings the action is set to the SOURCE static field. 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...

ViewFavorites:

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

AppSettings:

Same thing as code above ^

It looks action can be null . Use reversed construct to avoid NPE.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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