当用户单击“后退”按钮时,我的应用程序崩溃了。
在我的应用中,我有3个相关 Activity :
在MainActivity中,用户可以点击列表中某个人员的姓名以转到PersonActivity,或者使用该人员姓名旁边的“+”按钮直接跳至AddPaymentActivity。在PersonActivity中,还有一个“+”按钮也可导致AddPaymentActivity。
Perons数据存储在实现Serializable接口(interface)的Java Person类中。启动 Activity 时,我通过以下方式转发人对象
intent.putExtra(MainActivity.PERSON_MESSAGE, person);
当我从MainActivity直接进入AddPaymentActivity(通过“+”)时,后退按钮可以正常工作。但是,如果我使用PersonActivity中的Start 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...
}
}
最佳答案
您的问题出在以下两行之一:
this.share = new BigDecimal(intent.getStringExtra(OverviewActivity.SHARE_MESSAGE));
this.totalSum = new BigDecimal(intent.getStringExtra(OverviewActivity.SUM_MESSAGE));
您的2个
getStringExtra
之一似乎返回null。关于java - 按下后退按钮时,Android应用程序崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/21094347/