I have two images: red light and green light. I have my own ListView, which I would like to display red light when the list item is inactive, and green light when it is active. The list item is activated when clicked.
Here is my code
row.xml
<ImageView android:id="@+id/iconLight" android:src="@drawable/light_off" android:layout_width="wrap_content" android:layout_height="wrap_content"/>
main.java
ImageView iconLight = (ImageView)findViewById(R.id.iconLight); iconLight.setImageResource(R.drawable.light_on);
I get a NullPointerException throwing a string specifying an image resource. So I did a little testing, I deleted the line specifying src in the XML file, and just tried installing it in the main class. Another NPE. Therefore, I tried not to change the resource, but simply changed the alpha. Another NPE.
I'm not sure what I'm doing wrong. The files light_off.png and light_on.png are in res/drawable-ldpi , and each of them works when I specify them in XML. But any change that I try to make in iconLight in the main file causes this NPE. Any ideas?
source share