0

我有一个带有行(列表项)的自定义布局的 ListView,并且像往常一样使用适配器填充列表。在这个自定义布局中,我添加了一个 ImageView 和 TextView(所以有一个图标来匹配文本)。我无法根据 ListView 中的位置获取 ImageView(需要这样做,以便我可以将图标设置为明显匹配文本),我从错误日志中获得的唯一真实信息是错误所在的行但无论如何我都会发布它。任何帮助是极大的赞赏。我真的不想因为必须删除 ListView、创建一堆 RelativeLayouts、将它们包含在 LinearLayout 中并将其放在 Scrollview 中而造成混乱。

相关代码:-

ListView listView = (ListView) findViewById(R.id.servicelist);
String[] values = new String[] { "Choice0", "Choice1", "Choice2",
        "Choice3", "Choice4", "Choice5" };

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.services_row,
android.R.id.text1, values);

listView.setAdapter(adapter);

for(int position=0; position<11; position++){
    ViewGroup rowView = (ViewGroup) listView.getChildAt(position);
    ImageView icon = (ImageView) rowView.findViewById(R.id.service_row_icon); //ERROR HERE
    switch(position){
    case 0:{ icon.setImageResource(R.drawable.ic_launcher); break; }
    case 1:{ icon.setImageResource(R.drawable.ic_launcher); break; }
    //ETC ETC More cases go here, irrelevant code
    }
}

行布局(service_row.xml):-

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
    android:id="@+id/service_row_icon"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/icon"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"/>

<TextView
    android:id="@+id/service_row_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textColor="#33b5e5"
    android:paddingLeft="5dp"/>

</LinearLayout>

错误日志:-

06-28 01:46:31.581: E/AndroidRuntime(18070): FATAL EXCEPTION: main
06-28 01:46:31.581: E/AndroidRuntime(18070): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.dragonheart.autodroid/com.dragonheart.autodroid.ServiceClasses.AddServiceActivity}: java.lang.NullPointerException
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1968)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1993)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.access$600(ActivityThread.java:127)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1159)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.os.Looper.loop(Looper.java:137)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.main(ActivityThread.java:4507)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at java.lang.reflect.Method.invokeNative(Native Method)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at java.lang.reflect.Method.invoke(Method.java:511)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:980)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:747)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at dalvik.system.NativeStart.main(Native Method)
06-28 01:46:31.581: E/AndroidRuntime(18070): Caused by: java.lang.NullPointerException
06-28 01:46:31.581: E/AndroidRuntime(18070):    at com.dragonheart.autodroid.ServiceClasses.AddServiceActivity.onCreate(AddServiceActivity.java:43)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.Activity.performCreate(Activity.java:4465)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1052)
06-28 01:46:31.581: E/AndroidRuntime(18070):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1932)
06-28 01:46:31.581: E/AndroidRuntime(18070):    ... 11 more
4

1 回答 1

1

您需要在getView适配器内部而不是在适配器外部执行此操作。它崩溃是因为框架不知道您想要列表中的许多视图中的哪一个。

在适配器内部,它将知道位置并能够返回正确的视图。

于 2012-06-27T22:13:06.687 回答