当用户点击后退按钮时,我的应用中遇到了令人讨厌的崩溃。
在我的应用中,我有3个相关活动:
在MainActivity中,用户可以点击列表中的Person名称转到PersonActivity,或者使用Person名称旁边的“+”按钮直接跳转到AddPaymentActivity。在PersonActivity中,还有一个“+”按钮,也可以导致AddPaymentActivity。
Perons数据存储在实现Serializable接口的Java Person类中。当启动Activty时,我通过
转发Person对象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...
}
}
答案 0 :(得分:1)
您的问题出在以下两行之一:
this.share = new BigDecimal(intent.getStringExtra(OverviewActivity.SHARE_MESSAGE));
this.totalSum = new BigDecimal(intent.getStringExtra(OverviewActivity.SUM_MESSAGE));
您的2 getStringExtra
中的一个似乎返回null。
答案 1 :(得分:0)
一定是因为空指针异常会导致您的应用崩溃。
首先,检查PersonActivity中是否需要从AddPaymentActivity按下后退按钮时未传递/设置的值。
您也可以覆盖onResume()函数,按下后退按钮时会调用它。
答案 2 :(得分:0)
检查您是否添加了finish();在您的意图之后。您希望从上一个活动返回的意图/活动,在将此意图添加到父活动时,请确保调用此意图的父活动没有finish();.例如:
主要活动呼叫付款活动:
Intent d = new Intent(getApplicationContext(),
PaymentActivity.class);
startActivity(d);
finish();***//remove this***
return true;
如果你告诉活动调用另一个活动来完成(),这基本上意味着被调用的活动可以返回上一个活动,因此下面的内容是正确的: // finish();已被删除
Intent d = new Intent(getApplicationContext(),
PaymentActivity.class);
startActivity(d);
return true;
您需要做的下一件事是确保您的清单中已将父活动添加到活动中。
例如:
<activity
android:name=".PaymentActivity"
android:label="@string/paymentlabel"
android:parentActivityName=".PaymentActivity" />
在您的付款活动onCreate add中,添加以下内容:
assert getSupportActionBar() != null;***> //this tells the application that ActionBar is not null and therefore dont call an java null exceptions and crash the activity
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);