I have a ListView with a custom layout for the rows (list items) and the list is being populated with an adapter as usual. In this custom layout, I've added an ImageView and TextView (so there's an icon to match the text). I'm unable to get the ImageView based on the position in the ListView (Need to do this so I can set the icon to match the text obviously)and the only real information I get from the error log is the line the error is on but I'll post it anyways. Any help is greatly appreciated. I really don't want to create a mess by having to remove the ListView, create a bunch of RelativeLayouts, encompass them in a LinearLayout and put that inside a Scrollview.
Relevant Code:-
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
}
}
Row layout (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>
Error log:-
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
You need to do that inside your getView
of your adapter, not outside of it. It crashes because the framework doesn't have a clue which of the many views in your list that you want.
Inside the adapter, it will know the position and be able to return the proper view.