0
我有我的第二個活動中的onCreate()中調用以下方法。通過可串行化崩潰發送一個對象
它通過Serializable從第一個活動中獲取一個對象,並根據它創建一個列表,並更改該對象並將其發送回第一個活動。
這工作正常,但如果我發送回第一個活動未更改對象嘗試在第二個活動中創建列表時它崩潰。
private void createLectureListView() {
ListView lecListView = (ListView) findViewById(R.id.lectureListView);
Intent i = getIntent();
mod = (Module)i.getSerializableExtra("moduleObject");
lectureList = new ArrayList<Lecture>();
if (mod.lectureArrayList!=null) lectureList=mod.lectureArrayList;
listAdapter = new ArrayAdapter<Lecture>(this,android.R.layout.simple_list_item_1, lectureList);
lecListView.setAdapter(listAdapter);
}
這是例外:
Caused by: java.lang.NullPointerException
[編輯]添加的其它位。這是Activity1中的一個:
private void registerClickCallback() {
ListView listView1 = (ListView) findViewById(R.id.listView1);
listView1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent myIntent = new Intent(MenuActivity.this, LectureNotes.class);
//pass the module object we are selecting
myIntent.putExtra("moduleObject",modulesInfo.get(position));
pos = position;
startActivityForResult(myIntent,1);
}
});
}
第一個ACtivity中的onActivityResult方法。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == 1) {
if(resultCode == RESULT_OK){
mod = (Module)data.getSerializableExtra("moduleObject");
if (mod!=modulesInfo.get(pos))
modulesInfo.set(pos,mod);
}
}
}
[EDIT2]堆棧跟蹤
12-12 08:56:37.041 4143-4143/com.example.learnorizeapp E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.learnorizeapp/com.example.learnorizeapp.LectureNotes}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.example.learnorizeapp.LectureNotes.createLectureListView(LectureNotes.java:59)
at com.example.learnorizeapp.LectureNotes.onCreate(LectureNotes.java:28)
at android.app.Activity.performCreate(Activity.java:5104)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
at android.app.ActivityThread.access$600(ActivityThread.java:141)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
12-12 08:56:37.057 459-709/system_process W/ActivityManager﹕ Force finishing activity com.example.learnorizeapp/.LectureNotes
12-12 08:56:37.057 459-709/system_process W/ActivityManager﹕ Force finishing activity com.example.learnorizeapp/.MenuActivity
「崩潰」表明你的日誌中可能有一個例外 - 找到它,看看是否解釋它。如果您仍然不知道錯在哪裏,請編輯問題以包含例外。 –
描述中缺少一些內容:Act1通過intent向Act2發送對象,然後Act2將未更改的對象發送回Act1,並且「嘗試在第二個活動中創建列表時發生崩潰」。不過,在這一點上,你應該回到Act1。請說明哪些活動崩潰以及在哪裏,以及如何調用它們中的每一個:是通過startActivity()還是startActivityForResult()? – Melquiades
@Melquiades當我再次點擊Act1列表視圖中的某個項目再次打開ACt2時,它將停止。我認爲這是因爲它無法再在第2幕中創建列表視圖。使用startActivityForResult(),我已經複製了問題中的代碼。 – user3076606