带有ImageButtons数组的NullPointerException

时间:2014-01-23 10:03:50

标签: android arrays layout nullpointerexception imagebutton

我手动声明了一个ImageButton数组,然后手动添加了一些ImageButtons,在将这些ImageButtons分配给XML布局后,我尝试使用for来遍历该数组循环,但我在迭代该数组时得到NullPointerException(Exception带有第一个迭代它甚至不管理迭代一次),这是我的代码:

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
        }

和XML中的ImageButton

                            <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 ..!>

这就是LogCat结果:

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)

3 个答案:

答案 0 :(得分:5)

首先尝试充气然后添加到数组中,如:

 Antenna = (ImageButton) findViewById(R.id.antenna);
 ...
 ...
 public ImageButton[] upControllingButtons = {Antenna, AV, Components, VGA, HDMI, Switch};

答案 1 :(得分:0)

public ImageButton[] upControllingButtons = new ImageButton[7];
Antenna = (ImageButton) findViewById(R.id.antenna);
//Then Do your work

希望这会有所帮助

答案 2 :(得分:0)

您的approch错误不要为每个ImageButton使用单独的行来初始化您可以使用循环。

试试这个。

您必须按数字给出ImageButton的ID。像

<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
}