Android: getting NullPointerException for ImageView imag = (ImageView) findViewById (R.id.image)

I get a Null Pointer exception for ImageView imag = (ImageView) findViewById (R.id.image). I have a custome layout declared in R.layout.screen3 with a list of custome declared in R.layout.custom_row_screen3 . Each line has several tasks and, depending on their state, the image is displayed to the right of each line.

Please help me in solving this problem, 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 Traction

+1
android imageview
source share
5 answers

Thanks to all of you. You all pointed me in the right direction that my image does not point to my adapter. I have completed the following steps: http://devblogs.net/2011/01/04/custom-listview-with-image-using-simpleadapter/ and it works !!! it was just necessary to put my image in hashmap ... I hit my head against the wall!

0
source share

In short: you should write a ListAdapter that draws the views in a list. It seems you are using findViewById() in the wrong view / context (on layout x3 of the screen). An example containing a ListAdapter is available on the Android developer site.

The getView() method should 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; } 
+1
source share

It seems that you did not specify android: id = "@ + id / image" in the image view inside your layout

Edit: I think the problem is that you use findViewById before the ListView in ListActivity contains your custom_row that has an ImageView.

Try to execute setListAdapter before populateList ()

0
source share

check if the resource available for your laoding in the res folder is available or not.

0
source share

populateList () is run before the ListView is displayed on the screen, so custom_row_screen3 does not exist in Activity.

You can subclass SimpleAdapter and override getView () or setViewImage () , but it is often easier for me to anonymously implement a SimpleAdapter.ViewBinder and assign it to SimpleAdapter # setViewBinder () .

In the ViewBinder, return false if Past View is not an ImageView. If so, you can use the data card (passed as an object) to check the value of the "taskStatus" key.

Be 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); } }); 
0
source share

All Articles