Try inflating first then add to the array like:
Antenna = (ImageButton) findViewById(R.id.antenna);
...
...
public ImageButton[] upControllingButtons = {Antenna, AV, Components, VGA, HDMI, Switch};
Question
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)
La solution
Try inflating first then add to the array like:
Antenna = (ImageButton) findViewById(R.id.antenna);
...
...
public ImageButton[] upControllingButtons = {Antenna, AV, Components, VGA, HDMI, Switch};
Autres conseils
public ImageButton[] upControllingButtons = new ImageButton[7];
Antenna = (ImageButton) findViewById(R.id.antenna);
//Then Do your work
hope this will help
Your approch is wrong do not use separate line for each ImageButton to initialize you can use loop instead.
try this.
You have to give the ids of your ImageButton by numbers. like
<ImageButton
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
ArrayList<ImageButton> upControllingButtons = new ArrayList<ImageButton>();
for(int i=0; i<3; i++)
{
upControllingButtons .add(getImageButton(i));
}
private ImageButton getImageButton(int i)
{
int id=0;
try {
id = R.id.class.getField("button" + i).getInt(0);
}
catch (IllegalArgumentException e){
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch(IllegalAccessException e) {
e.printStackTrace();
} catch (NoSuchFieldException e) {
e.printStackTrace();
}
ImageButton imageButton = (ImageButton)findViewById(id);
return imageButton
}