アプリがバックグラウンドにあり、フォアグラウンドに移動したときにメイン アクティビティを再度作成する必要がある場合に、null ポインター例外が発生します。以下のコードは、オーバーライドされたアクティビティ メソッド onCreateOptionsMenu のコードです。ここでは、データが失われた場合に SplashActivity でアプリを再起動するようにいくつかの null チェックを行います。デバッグ ビルドは正常に動作しますが、リリース バージョンをビルドすると、アプリケーションが停止したときにアプリをフォアグラウンドに移動した後、以下の例外が一貫して発生します。理解していない。これはプロガードの問題ではないと思います。
@Override
public boolean onCreateOptionsMenu(Menu menu) {
if (agencyManager == null || menu == null || getMenuInflater() == null) {
// If agencyManager data lost (possibly caused by backgrounding the app for a long period of time),
// launch the splash activity, pass through any potential stop selected
// from a notification, and finish.
Intent intent = new Intent(MapActivity.this, SplashActivity.class);
if (stopSelectedFromNotification != -1) {
intent.putExtra(STOP_ID_EXTRA, stopSelectedFromNotification);
intent.putExtra(ROUTE_ID_EXTRA, routeSelectedFromNotification);
}
startActivity(intent);
overridePendingTransition(0, 0); // Don't animate the transition
finish();
return false;
}
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
MenuItem twitter = menu.findItem(R.id.action_news);
twitter.setVisible(!agencyManager.getTwitter().equals(""));
MenuItem website = menu.findItem(R.id.action_website);
website.setVisible(!agencyManager.getURL().equals(""));
MenuItem faq = menu.findItem(R.id.action_faq);
faq.setVisible(!agencyManager.getFaqURL().equals(""));
MenuItem riding = menu.findItem(R.id.action_riding);
riding.setVisible(!agencyManager.getRidingURL().equals(""));
MenuItem code = menu.findItem(R.id.action_code);
code.setVisible(!agencyManager.getCodeURL().equals(""));
MenuItem fares = menu.findItem(R.id.action_fares);
fares.setVisible(agencyManager.getFareJSON().length()>5 || agencyManager.getFareText().length()>5);
/**
//Webschedule view option added to menu, will be visible if schedule table supported
MenuItem webSchedule = menu.findItem(R.id.action_web_schedule);
final Route route = mapUiManager.selectedRoute;
webSchedule.setVisible(agencyManager.useScheduleTable());
**/
MenuItem settings = menu.findItem(R.id.action_settings);
// Display the welcome info screens the first time the app is run
displayWelcomeInfoFirstRun();
return true;
}
ログ:
01-18 12:48:28.872 31937-31937/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: edu.company.android.packagename, PID: 31937
java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String edu.company.android.packagename.a.g()' on a null object reference
at edu.company.android.packagename.MapActivity.onCreateOptionsMenu(Unknown Source)
at android.app.Activity.onCreatePanelMenu(Activity.java:2850)
at android.support.v4.app.aa.onCreatePanelMenu(Unknown Source)
at android.support.v7.a.f.a(Unknown Source)
at android.support.v7.a.l.a(Unknown Source)
at android.support.v7.a.f.onCreatePanelMenu(Unknown Source)
at android.support.v7.a.m.onCreatePanelMenu(Unknown Source)
at com.android.internal.policy.PhoneWindow.preparePanel(PhoneWindow.java:567)
at com.android.internal.policy.PhoneWindow.doInvalidatePanelMenu(PhoneWindow.java:939)
at com.android.internal.policy.PhoneWindow$1.run(PhoneWindow.java:271)
at android.os.Handler.handleCallback(Handler.java:746)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)