Note that there are some explanatory texts on larger screens.

plurals
  1. POAndroid: Having trouble getting View in a ViewGroup used to create ListView row layouts
    text
    copied!<p>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.</p> <p>Relevant Code:-</p> <pre><code>ListView listView = (ListView) findViewById(R.id.servicelist); String[] values = new String[] { "Choice0", "Choice1", "Choice2", "Choice3", "Choice4", "Choice5" }; ArrayAdapter&lt;String&gt; adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.services_row, android.R.id.text1, values); listView.setAdapter(adapter); for(int position=0; position&lt;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 } } </code></pre> <p>Row layout (service_row.xml):-</p> <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="horizontal"&gt; &lt;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"/&gt; &lt;TextView android:id="@+id/service_row_text" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#33b5e5" android:paddingLeft="5dp"/&gt; &lt;/LinearLayout&gt; </code></pre> <p>Error log:-</p> <pre><code>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 </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload