1

I am getting Null Pointer Exception for ImageView imag = (ImageView) findViewById(R.id.image). I have custome layout declared in R.layout.screen3 with custome list declared in R.layout.custom_row_screen3. Each row has few tasks and depending on their status an image is shown to right of each row.

Please guide me in getting this resolved as I am new to android.

here is my code below :::

public class screen3 extends ListActivity{
final ArrayList<HashMap<String, String>> list = new ArrayList<HashMap<String, String>>();
...
...
...
TextView tv;
ImageView imag;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.screen3);   
    System.out.println("Screen3");
    ...
    ...
    ...
    SimpleAdapter adapter = new SimpleAdapter(this, list,
            R.layout.custom_row_screen3,
            new String[] { "tname","schedule", "note", "time"}, new int[] { R.id.text1Screen3task,
                    R.id.text2Screen3task, R.id.text3Screen3task, R.id.text4Screen3task});
    populateList();
    setListAdapter(adapter);
}

public void populateList() {
    for(int i = 0; i <numberOfTasks; i++)
    {
        HashMap<String, String> h = new HashMap<String, String>();
        HashMap<String, String> hm = new HashMap<String, String>();
        h = tasks.get(i);
            ...
            ...     
        hm.put("tname", h.get("taskName"));
        hm.put("schedule", h.get("STime")+" - "+h.get("ETime"));

        if(h.get("taskStatus").trim().equals("1")){

            imag = (ImageView) findViewById(R.id.image);//   <<---- NULL RETURNED
            imag.setImageResource(R.drawable.done);
            ...
            ...
        }
        else{
            imag = (ImageView) findViewById(R.id.image);//  <<---- NULL RETURNED
            imag.setImageResource(R.drawable.not);
            ...
            ...
        }
        list.add(hm);
    }
    ...
    ... 
}

screen3 xml

<ListView android:id="@id/android:list" android:layout_width="fill_parent"
    android:layout_height="wrap_content" android:background="#000000"
    android:drawSelectorOnTop="false">
</ListView>

custom_row_screen3 xml

<LinearLayout android:orientation="vertical"
    android:layout_width="0dip" android:layout_weight="1"
    android:layout_height="wrap_content">

    <TextView android:id="@+id/text1Screen3task"
        android:layout_width="wrap_content" android:layout_height="0dip"
        android:layout_weight="1" android:textSize="21sp" android:textStyle="bold"
        android:gravity="clip_horizontal" />

    <TextView android:id="@+id/text2Screen3task"
        android:layout_width="wrap_content" android:layout_height="0dip"
        android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />

    <TextView android:id="@+id/text3Screen3task"
        android:layout_width="wrap_content" android:layout_height="0dip"
        android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />

    <TextView android:id="@+id/text4Screen3task"
        android:layout_width="wrap_content" android:layout_height="0dip"
        android:layout_weight="1" android:textSize="16sp" android:gravity="clip_horizontal" />
</LinearLayout>
<ImageView android:id="@+id/image" android:layout_width="wrap_content" android:paddingBottom="51dip"
    android:layout_height="wrap_content" android:layout_marginRight="16dip"
    />

Thanks Abhinav Tyagi

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60
  • 1
    Are you sure your screen3 layout have an ImageView naming as image? If yes can you post your screen3.xml codes? – Pankaj Kumar May 02 '11 at 10:59
  • 05-02 15:45:18.006: ERROR/AndroidRuntime(488): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.scandihealth/com.scandihealth.screen3}: java.lang.NullPointerException 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread.access$2300(ActivityThread.java:125) – Abhinav Tyagi May 02 '11 at 11:09
  • 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.os.Handler.dispatchMessage(Handler.java:99) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.os.Looper.loop(Looper.java:123) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread.main(ActivityThread.java:4627) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at java.lang.reflect.Method.invokeNative(Native Method) – Abhinav Tyagi May 02 '11 at 11:10
  • 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at java.lang.reflect.Method.invoke(Method.java:521) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at dalvik.system.NativeStart.main(Native Method) – Abhinav Tyagi May 02 '11 at 11:10
  • 05-02 15:45:18.006: ERROR/AndroidRuntime(488): Caused by: java.lang.NullPointerException 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at com.scandihealth.screen3.populate(screen3.java:141) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at com.scandihealth.screen3.onCreate(screen3.java:49) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 05-02 15:45:18.006: ERROR/AndroidRuntime(488): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) – Abhinav Tyagi May 02 '11 at 11:11
  • @pankaj : Screen3 xml is not having imageView it is in custom_row. I am populating a custom list in screen3 and custom row has imageView. – Abhinav Tyagi May 02 '11 at 11:34
  • @AbhinavTyagi : put ur imageview inside linearlayout. – LostGeek May 19 '12 at 10:57
  • Do you use multiple layout resource? e.g. - layout/main.xml - layout-land/main.xml Have you specified theirs id? – user2104091 Feb 24 '13 at 08:59

5 Answers5

1

In short: You should write a ListAdapter which draws the views in the list. You seem to use findViewById() on the wrong view/context (on the screen3 xml layout). An example containing a ListAdapter is available at the android developer site.

The getView() method should basically look something like this:

public View getView(int position, View convertView, ViewGroup parent) {
    View v = convertView;
    if (convertView == null) {
        v = LayoutInflater.from(parent.getContext()).inflate(R.layout.row, null);
    }
    h = tasks.get(i);
    if(h.get("taskStatus").trim().equals("1")){
        imag = (ImageView) v.findViewById(R.id.image);
        imag.setImageResource(R.drawable.done);
    } else { 
        // ...
    }
    return v;
}
Stephan
  • 7,360
  • 37
  • 46
0

It seems that you haven't specified android:id="@+id/image" in the imageview inside your layout

Edit: I think the problem is you are using findViewById before the ListView in your ListActivity contains your custom_row which is who has the ImageView.

Try doing setListAdapter before populateList()

Rafaesp
  • 613
  • 6
  • 17
0

check whether the resource which your laoding is available in your res folder or not.

krupa parekh
  • 500
  • 3
  • 7
0

populateList() runs before the ListView is rendered to the screen, so the custom_row_screen3 doesn't exist in the Activity.

You could subclass SimpleAdapter and override getView() or setViewImage(), but I often find it simpler to anonymously implement a SimpleAdapter.ViewBinder and assign it with SimpleAdapter#setViewBinder().

In the ViewBinder, return false if the View that's passed in isn't an ImageView. If it is, you can use the map of data (passed in as an Object) to check the value of the "taskStatus" key.

Make sure to do this before calling setListAdapter().

adapter.setViewBinder(new SimpleAdapter.ViewBinder () {
  @Override
  boolean setViewValue(View view, Object data, String textRepresentation) {
    if (!(view instanceof ImageView)) return false;

    String taskStatus = ((Map<String, String>) data).get("taskStatus").trim();
    ((ImageView) view).setImageResource(taskStatus.equals("1") ? R.drawable.done :
                                                               R.drawable.not);
  }
});
Mwanji Ezana
  • 924
  • 8
  • 15
  • Thanks for helping me out but its total confusion for me right now!!! I am a novice in android and not used Binders and all. – Abhinav Tyagi May 02 '11 at 12:05
  • not working SimpleAdapter adapter = new SimpleAdapter(.....); adapter.setViewBinder(new SimpleAdapter.ViewBinder () { @SuppressWarnings("unchecked") @Override public boolean setViewValue(View view, Object data, String textRepresentation) { if (!(view instanceof ImageView)) return false; String ts = ((HashMap) data).get("taskStatus").trim(); ((ImageView) view).setImageResource(ts.equals("1") ? R.drawable.done :R.drawable.not); return true; } }); populateList(); setListAdapter(adapter); – Abhinav Tyagi May 02 '11 at 12:37
  • @Abhinav: by "not working", do you mean nothing is showing up, that an exception is thrown or that it doesn't compile? Also, try calling populateList() before creating SimpleAdapter – Mwanji Ezana May 02 '11 at 13:19
0

Thanks all of you You all all pointed me in right direction that my imageview is not pointing to my adapter I followed the steps as in http://devblogs.net/2011/01/04/custom-listview-with-image-using-simpleadapter/ and it was working!!! just needed to put my image in hashmap... I was banging my head to the wall!!!

Abhinav Tyagi
  • 5,158
  • 3
  • 30
  • 60