Skip to content Skip to sidebar Skip to footer

Android Imageview Nullpointerexception

I have two images, a red light and a green light. I have a custom ListView that I would like to display a red light when a list item is inactive, and a green light when it is activ

Solution 1:

The only way to get a NPE in the line...

iconLight.setImageResource(R.drawable.light_on);

Is for iconLight to be null. So, your findViewById is failing. Have you set your layout before you call findViewById? Are you sure R.id.iconLight is in the Activity's root layout?

Solution 2:

I had the same problem. Here is a code that helped me understand. It is for a Dialog window but might help you too.

finalDialogdialog=newDialog(context);
    dialog.setContentView(R.layout.custom);
    dialog.setTitle("Title...");
    TextViewtext= (TextView) dialog.findViewById(R.id.text);
    text.setText("Android");
    ImageViewimage= (ImageView) dialog.findViewById(R.id.image);
    image.setImageResource(R.drawable.ic_launcher);

Watch the line before the last one. Notice how it instantiates the ImageView. Anyway every change to the image is made after the setContentView.

Post a Comment for "Android Imageview Nullpointerexception"