关于这个问题已经有一些问题,但我找不到什么问题。我有一个带有ImageView和TextView的ListView。 listview使用item_layout.xlm,它包含两个元素。
我可以使用ArrayAdapter在ListView中填充TextView,但在尝试更改ImageView的源代码时出现NullPointerException。
private void populateListViewLongFronde() {
Cursor cursor = myDb.getAllDifEntries("long_fronde");
ListView myList = (ListView) findViewById(R.id.listView);
ArrayAdapter<String> arrayAdapter = new ArrayAdapter<String>(
this,
R.layout.item_layout,
R.id.textView3,
DBAdapter.AlResults );
myList.setAdapter(arrayAdapter);
ImageView myImage = (ImageView) findViewById(R.id.myImage);
myImage.setImageResource(R.drawable.deltoide);
}
这是Logcat:
09-15 02:38:50.319 1769-1769/? E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gobtron.database_test/com.gobtron.database_test.MainActivity}: java.lang.NullPointerException
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1956)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at com.gobtron.database_test.MainActivity.populateListViewLongFronde(MainActivity.java:119)
at com.gobtron.database_test.MainActivity.onCreate(MainActivity.java:44)
at android.app.Activity.performCreate(Activity.java:4466)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1920)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1981)
at android.app.ActivityThread.access$600(ActivityThread.java:123)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1147)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:4424)
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:784)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:0)
您无法使用ArrayAdapter存储ImageView和TextView的数据。您需要实现自己的列表适配器,并且可能需要扩展BaseAdapter。您的代码可能会为您提供空指针,因为您无法通过在活动中使用findViewByID来访问列表视图元素,您无论如何都不应该这样做。
答案 1 :(得分:0)
因为ImageView在item_layout.xml中,所以你应该这样做:
View view = LayoutInflater.from(activity).inflate(R.layout.item_layout, null);
ImageView imgView=(ImageView)view.findViewById(R.id.myImage);