在我的申请中,我将一个Arraylist从一个活动传递到另一个活动。 FlipCardActivity是父活动(活动A),RecipientsActivity是子活动(活动B)。我使用putExtra将空的RecipientArray(ArrayList)从A传递给B.在活动B中,我检查ArrayList的大小是否为零,如果它为零,则调用另一个活动(活动C),然后将项目添加到arraylist并将其返回到Activity B.在B中,我使用ListView来显示项目。我在活动B中有一个“后退”按钮,它完成B并重新启动活动A.再次当我点击EditText时,活动B就开始了。现在我仍然在ArrayList中有项目,但是当我将适配器设置为ListView以显示项目时,它会产生NullPointerException。我无法弄清楚为什么会出现这个错误,因为我在OnActivityResult中重复了相同的代码,并且它没有给我任何错误。
以下是我的代码的一部分:
在活动A中:
btn_rec = (ImageButton) findViewById(R.id.btn_rec);
btn_rec.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
Intent rec_Intent = new Intent(FlipCardActivity.this,
RecipientsActivity.class);
rec_Intent.putExtra("RecArray", RecipientArray);
startActivityForResult(rec_Intent, NO_OF_RECIPIENTS);
}
});
此处活动B在单击按钮btn_rec时启动。
在活动B中:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
RecipientArray = (ArrayList<Person>) getIntent().getSerializableExtra(
"RecArray");
Log.e("Recipient Array", "size = " + RecipientArray.size());
if (RecipientArray.size() == 0) { //activity C is started here
Intent rec_addr_Intent = new Intent(RecipientsActivity.this,
RecipientAddressActivity.class);
rec_addr_Intent.putExtra("RecArray", RecipientArray);
startActivityForResult(rec_addr_Intent, REC_INFO);
} else {
SharedPreferences prefs = PreferenceManager
.getDefaultSharedPreferences(this.getApplicationContext());
int size = prefs.getInt("size", 0);
for (int i = 0; i < size; i++) {
String json = prefs.getString("RecList_" + i, "");
Gson gson = new Gson();
Person p = gson.fromJson(json, Person.class);
RecipientArray.add(p);
}
// Log.e("RecListActivity","Size of arraylist"+RecipientArray.size());
this.m_adapter = new CustomListAdapter(RecipientsActivity.this,
R.layout.recipients_list, RecipientArray);
list = (ListView) findViewById(R.id.rec_list);
list.setAdapter(m_adapter);//I get NullPointer Exception here
}
setContentView(R.layout.activity_recipients);
addListenerForButtons();
}
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REC_INFO && resultCode == RESULT_OK && data != null) {
RecipientArray = (ArrayList<Person>) data
.getSerializableExtra("RecArray");
Log.e("Before for", "size = " + RecipientArray.size());
this.m_adapter = new CustomListAdapter(this,
R.layout.recipients_list, RecipientArray);
list = (ListView) findViewById(R.id.rec_list);
list.setAdapter(m_adapter);
}
与ListView相关的相同代码就在这里。
请帮助我尽快帮助我。如果我需要更多代码或Logcat信息,请告诉我。
Logcat文件:
01-06 11:52:55.998: E/AddrAdctivity(1278): On Pause
01-06 11:52:56.018: E/Before for(1278): size = 1
01-06 11:52:59.251: E/In onpause(1278): Hola
01-06 11:52:59.351: E/FlipCardActivity(1278): This activity is restarted
01-06 11:52:59.351: E/FlipCardActivity(1278): Size of arraylist 1
01-06 11:53:04.907: E/In edt onclick(1278): Hello
01-06 11:53:04.967: E/FlipCardActivity(1278): This activity is paused
01-06 11:53:04.997: E/Recipient Array(1278): size = 1
01-06 11:53:05.027: E/AndroidRuntime(1278): FATAL EXCEPTION: main
01-06 11:53:05.027: E/AndroidRuntime(1278): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.sample.postcare2/com.sample.postcare2.RecipientsActivity}: java.lang.NullPointerException
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2194)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2229)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread.access$600(ActivityThread.java:139)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1261)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.os.Handler.dispatchMessage(Handler.java:99)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.os.Looper.loop(Looper.java:154)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread.main(ActivityThread.java:4944)
01-06 11:53:05.027: E/AndroidRuntime(1278): at java.lang.reflect.Method.invokeNative(Native Method)
01-06 11:53:05.027: E/AndroidRuntime(1278): at java.lang.reflect.Method.invoke(Method.java:511)
01-06 11:53:05.027: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-06 11:53:05.027: E/AndroidRuntime(1278): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-06 11:53:05.027: E/AndroidRuntime(1278): at dalvik.system.NativeStart.main(Native Method)
01-06 11:53:05.027: E/AndroidRuntime(1278): Caused by: java.lang.NullPointerException
01-06 11:53:05.027: E/AndroidRuntime(1278): at com.sample.postcare2.RecipientsActivity.onCreate(RecipientsActivity.java:63)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.Activity.performCreate(Activity.java:4531)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1071)
01-06 11:53:05.027: E/AndroidRuntime(1278): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2150)
01-06 11:53:05.027: E/AndroidRuntime(1278): ... 11 more
答案 0 :(得分:1)
你在做什么
list = (ListView) findViewById(R.id.rec_list);
之前
setContentView(R.layout.activity_recipients);
请将setContentView移动到第一行,否则findViewById无法找到任何内容,这就是为什么你得到nullpointer