【发布时间】:2014-09-15 17:43:08
【问题描述】:
我有一个处理通知的服务。当我点击通知时,我正在向 Activity (NotificationActivity) 发送 Parcelable 对象:
服务:
Intent destIntent = new Intent (this, NotificationActivity.class);
destIntent.putExtra ("notificationData", new ParcelableObject (mes));
PendingIntent contentIntent = PendingIntent.getActivity (this, 0, destIntent, 0);
NotificationActivity.java:
public class NotificationActivity extends Activity {
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_notification);
// Always NullPointerException
ParcelableObject model = (ParcelableObject) (savedInstanceState.getParcelable ("notificationData"));
TextView content = (TextView)findViewById(R.id.content);
if (model == null) {
content.setText ("NULL");
} else {
content.setText (String.valueOf (model.dump ()));
}
}
}
但我在检索对象时一直出现 NullPointerException..
编辑: 遵循提供的答案后,我编辑了活动的代码,例如:
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_notification);
}
@Override
protected void onNewIntent (Intent intent) {
super.onNewIntent (intent);
setIntent (intent);
// Code not executed here
// Needed to move it to onResume ()
// since according to the doc it comes after onNewIntent ()
}
@Override
protected void onResume () {
super.onResume ();
ParcelableObject model = (ParcelableObject) (getIntent ().getParcelableExtra ("notificationData"));
Log.v ("MODEL :: ", model.dump().toString()); // NullPointerException
TextView content = (TextView)findViewById(R.id.content);
if (model == null) {
content.setText ("NULL");
} else {
content.setText (String.valueOf (model.dump ()));
}
}
有什么建议吗?谢谢。
【问题讨论】:
标签: android nullpointerexception parcelable