當用戶單擊後退按鈕時,我遇到了令人討厭的崩潰。按下後退按鈕時Android應用程序崩潰
在我的應用程序,我有3個相關的活動:
- MainActivity:顯示人的名單
- PersonActivity:顯示一個人的所有付款清單
- AddPaymentActivity:具有輸入元素向個人添加付款
從MainActivity中,用戶可以點擊列表中的個人姓名以轉到PersonActivity或使用Person's nam旁邊的「+」按鈕e並直接跳轉到AddPaymentActivity。在PersonActivity中,還有一個「+」按鈕,導致AddPaymentActivity。
Perons數據存儲在實現Serializable接口的Java Person類中。當啓動一個Activty我通過
intent.putExtra(MainActivity.PERSON_MESSAGE, person);
轉發人物對象,當我從MainActivity去直接進入AddPaymentActivity(通過「+」),後退按鈕正常工作。如果我然而使用從PersonActivity啓動AddPaymentActivity,我的應用程序崩潰與空指針異常。
使用調試器,我注意到當按下後退按鈕時,PersonActivity.onCreate()被調用(再次?)。爲什麼在返回形式AddPaymentActivity到PersonActivity時發生這種情況?爲什麼在從AddPaymentActivity返回MainActivity時不會發生這種情況?我能做些什麼來解決這個問題?
堆棧跟蹤:
01-13 10:11:01.423: E/AndroidRuntime(1022): FATAL EXCEPTION: main
01-13 10:11:01.423: E/AndroidRuntime(1022): Process: de.emteg.cashsplit, PID: 1022
01-13 10:11:01.423: E/AndroidRuntime(1022): java.lang.RuntimeException: Unable to start activity ComponentInfo{de.emteg.cashsplit/de.emteg.cashsplit.PersonActivity}: java.lang.NullPointerException
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2176)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2226)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread.access$700(ActivityThread.java:135)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1397)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.os.Handler.dispatchMessage(Handler.java:102)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.os.Looper.loop(Looper.java:137)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread.main(ActivityThread.java:4998)
01-13 10:11:01.423: E/AndroidRuntime(1022): at java.lang.reflect.Method.invokeNative(Native Method)
01-13 10:11:01.423: E/AndroidRuntime(1022): at java.lang.reflect.Method.invoke(Method.java:515)
01-13 10:11:01.423: E/AndroidRuntime(1022): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:777)
01-13 10:11:01.423: E/AndroidRuntime(1022): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:593)
01-13 10:11:01.423: E/AndroidRuntime(1022): at dalvik.system.NativeStart.main(Native Method)
01-13 10:11:01.423: E/AndroidRuntime(1022): Caused by: java.lang.NullPointerException
01-13 10:11:01.423: E/AndroidRuntime(1022): at java.math.BigDecimal.<init>(BigDecimal.java:425)
01-13 10:11:01.423: E/AndroidRuntime(1022): at de.emteg.cashsplit.PersonActivity.onCreate(PersonActivity.java:37)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.Activity.performCreate(Activity.java:5243)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
01-13 10:11:01.423: E/AndroidRuntime(1022): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2140)
01-13 10:11:01.423: E/AndroidRuntime(1022): ... 11 more
MainActivity.onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_overview);
persons = new ArrayList<Person>();
// Populate list with some test person objects
Person p = new Person("Frank");
p.getPayments().add(new Payment("test1", new BigDecimal(5.7)));
persons.add(p);
p = new Person("Peter");
persons.add(p);
ListView list = (ListView) findViewById(R.id.listView1);
PersonAdapter adapter = new PersonAdapter();
list.setAdapter(adapter);
// output....
}
PersonActivity.onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_person);
Intent intent = getIntent();
this.person = (Person) intent.getSerializableExtra(MainActivity.PERSON_MESSAGE);
this.share = new BigDecimal(intent.getStringExtra(MainActivity.SHARE_MESSAGE));
this.totalSum = new BigDecimal(intent.getStringExtra(MainActivity.SUM_MESSAGE));
this.diff = person.getSum().subtract(share);
setupActionBar();
// output...
}
AddPaymentActivty.onCreate():
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_payment);
// Show the Up button in the action bar.
setupActionBar();
Intent intent = getIntent();
this.person = (Person) intent.getSerializableExtra(MainActivity.PERSON_MESSAGE);
if (intent.hasExtra(MainActivity.PAYMENT_MESSAGE)) {
this.payment = (Payment) intent.getSerializableExtra(MainActivity.PAYMENT_MESSAGE);
// output....
} else {
this.payment = null;
// output...
}
}
嗯,是的,如果你能後的堆棧跟蹤,這將是巨大的。 – keyboardsurfer
看起來你的活動在你調用它時彈出堆棧,發佈你創建的類的方法,你崩潰了。通過你的日誌,似乎傳遞的東西實際上從來沒有通過 – Saqib
在PersonActivity.onCreate()崩潰發生時這行的代碼被執行: '意圖意圖= getIntent();'\t 'this.person =(人)intent.getSerializableExtra(MainActivity.PERSON_MESSAGE);' 但我認爲,在按下返回按鈕不會再次完全創建活動。它不會創建MainActivity。崩潰是合乎邏輯的,因爲後退按鈕沒有放置額外功能。但爲什麼onCreate會發生呢? – Matthias