I declared an ImageButton array manually and then add some ImageButtons manually to it, and after assigning these ImageButtons to the XML layout, I tried to iterate through that array using a for loop, but I'm getting a NullPointerException when iterating through that array(the Exception comes with the first Iterate it's not even manage the iteration once), that's my code:
public ImageButton Antenna, AV, Components, VGA, HDMI, Switch;
public ImageButton[] upControllingButtons = {Antenna, AV, Components, VGA, HDMI, Switch};
Antenna = (ImageButton) findViewById(R.id.antenna);
// then the other six ImageButtons
// setting the onClick for the Up_Controlling butoons
for(int k=0; k < upControllingButtons.length ; k++ ){
upControllingButtons[k].setTag("tag"); // i got the Exception here
}
and the ImageButton in the XML:
<ImageButton
android:id="@+id/antenna"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginLeft="7dp"
android:layout_marginRight="7dp"
android:adjustViewBounds="true"
android:background="@drawable/remote_living_buttons"
android:clickable="true"
android:padding="8dp"
android:scaleType="centerInside"
android:src="@drawable/remote_living_antena"
android:tag="released" />
<!.. then the other six ..!>
and that is the LogCat result:
01-23 11:43:11.068: ERROR/AndroidRuntime(589): FATAL EXCEPTION: main
01-23 11:43:11.068: ERROR/AndroidRuntime(589): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.solaceap/com.example.solaceap.RemoteTV}: java.lang.NullPointerException
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread.access$600(ActivityThread.java:122)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.os.Handler.dispatchMessage(Handler.java:99)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.os.Looper.loop(Looper.java:137)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread.main(ActivityThread.java:4340)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at java.lang.reflect.Method.invokeNative(Native Method)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at java.lang.reflect.Method.invoke(Method.java:511)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at dalvik.system.NativeStart.main(Native Method)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): Caused by: java.lang.NullPointerException
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at com.example.solaceap.RemoteTV.get_available_devices(RemoteTV.java:221)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at com.example.solaceap.RemoteTV.onCreate(RemoteTV.java:81)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.Activity.performCreate(Activity.java:4465)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
01-23 11:43:11.068: ERROR/AndroidRuntime(589): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
findViewByIdon all 6, so does initialise them all. Its not clear though due to him having hid the code.