我在我的主要活动中将arrayList传递给另一个活动:
Intent i = new Intent(getApplicationContext(),PairedDevices.class);
i.putExtra("arrayListPaired", arrayListpaired);
startActivity(i);
要传递给名为PairedDevices的actvity,我要显示该数组列表。这是PairedDevices类:
public class PairedDevices extends Activity {
ListView listViewPaired;
ArrayList<String> pairedDevices = (ArrayList<String>) getIntent().getSerializableExtra("arrayListPaired");
ArrayAdapter<String> listAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paired_devices);
listViewPaired = (ListView)findViewById(R.id.listViewPaired);
listAdapter = new ArrayAdapter<String>(PairedDevices.this, android.R.layout.simple_list_item_1, pairedDevices);
listViewPaired.setAdapter(listAdapter);
}
}
它崩溃了,我得到了以下的堆栈跟踪,如何解决这个问题的任何建议将非常感谢!:
10-28 02:04:10.101 25536-25536/my.obd2connector E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: my.obd2connector, PID: 25536
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{my.obd2connector/my.obd2connector.PairedDevices}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2231)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at my.obd2connector.PairedDevices.<init>(PairedDevices.java:17)
at java.lang.Class.newInstanceImpl(Native Method)
at java.lang.Class.newInstance(Class.java:1208)
at android.app.Instrumentation.newActivity(Instrumentation.java:1079)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2222)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
将此代码放在onCreate();
中ArrayList<String> pairedDevices = (ArrayList<String>) getIntent().getSerializableExtra("arrayListPaired");
答案 1 :(得分:1)
将其放入onCreate
pairedDevices = (ArrayList<String>) getIntent().getSerializableExtra("arrayListPaired");
像这样
public class PairedDevices extends Activity
{
ListView listViewPaired;
ArrayList<String> pairedDevices = null;
ArrayAdapter<String> listAdapter;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_paired_devices);
listViewPaired = (ListView)findViewById(R.id.listViewPaired);
pairedDevices = (ArrayList<String>)getIntent().getSerializableExtra("arrayListPaired");
listAdapter = new ArrayAdapter<String>(PairedDevices.this, android.R.layout.simple_list_item_1, pairedDevices);
listViewPaired.setAdapter(listAdapter);
}
}
答案 2 :(得分:1)
替换此代码以解决您的错误
Intent i = new Intent(getApplicationContext(),PairedDevices.class);
i.putStringArrayListExtra(&#34; arrayListPaired&#34;,arrayListpaired);
startActivity(ⅰ);
此代码替换为PairedDevices文件
的ArrayList&LT; String
&GT; pairedDevices =(ArrayList&lt; String
&gt;)getIntent()。getStringArrayListExtra(&#34; arrayListPaired&#34;);