我有第二个活动在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
答案 0 :(得分:0)
讲座有一个问题在做一些不同的场景时,例如点击一个按钮但没有点击另一个,它返回null。