我正在尝试使用BaseAdapter创建一个列表视图,但是在向ArrayList添加对象的时候一直收到Null Object Reference错误。
相关代码如下:
public class MainActivity extends ActionBarActivity {
int[] icons = {R.drawable.icon01, R.drawable.icon02, R.drawable.icon03, R.drawable.icon04, R.drawable.icon05, R.drawable.icon06, R.drawable.icon07, R.drawable.icon08, R.drawable.icon09, R.drawable.icon10, R.drawable.icon11, R.drawable.icon12};
ListView listView;
String[] title, desc;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listView);
Resources resources = getResources();
title = resources.getStringArray(R.array.titles);
desc = resources.getStringArray(R.array.desc);
RT_Adapter rt_adapter = new RT_Adapter(this, title, desc, icons);
listView.setAdapter(rt_adapter);
}
}
class Creator {
String a, b;
int c;
Creator(String d, String e, int f) {
this.a = d;
this.b = e;
this.c = f;
Log.d("RT", a +" - "+b+" - "+c); //Position 01 - This is Description 01 - 2130837556
}
}
class RT_Adapter extends BaseAdapter {
Context c;
ArrayList<Creator> list;
public RT_Adapter(Context context, String[] title, String[] descs, int[] icons) {
this.c = context;
for (int i = 0; i < title.length; i++) {
list.add(new Creator(title[i], descs[i], icons[i]));
}
}
...
}
我在下面这行代码中遇到错误(通过调试发现):
list.add(new Creator(title[i], descs[i], icons[i]));
以下是Logcat中的实际错误:
02-09 17:09:05.494 767-767/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.rt.droid.baseadapter01, PID: 767
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rt.droid.baseadapter01/com.rt.droid.baseadapter01.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'boolean java.util.ArrayList.add(java.lang.Object)' on a null object reference
at com.rt.droid.baseadapter01.RT_Adapter.<init>(MainActivity.java:71)
at com.rt.droid.baseadapter01.MainActivity.onCreate(MainActivity.java:36)
at android.app.Activity.performCreate(Activity.java:5933)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
at android.app.ActivityThread.access$800(ActivityThread.java:144)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5221)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:898)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:693)
我不明白为什么在创建新的Creator对象以添加到ArrayList时,我会得到一个空对象。
如果有任何提示,将不胜感激。
ArrayList<Creator> list;
没有被初始化。 - Nirbhay Kundanthis.c = context;
下面添加this.list = new ArrayList<Creator>();
。 - Kevin Cruijssen