2014-01-23 65 views
0

我手動聲明的ImageButton數組,然後手動添加一些ImageButtons給它,並賦予這些ImageButtons到XML佈局後,我試圖通過使用一個for循環,數組迭代,但我得到一個NullPointerException通過數組迭代時(例外自帶它甚至沒有管理迭代一旦第一迭代),這是我的代碼:NullPointerException異常與ImageButtons陣列

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) 
+4

@Raghunandan如果你正確地閱讀代碼(和他的評論),他使用'findViewById'在所有6,所以它們全部初始化。它不清楚,但由於他隱藏了代碼。 – Doomsknight

+0

您是否爲您的活動設置了contentView? –

+0

發佈完整的代碼。它不清楚你在做什麼 – Raghunandan

回答

5

嘗試充氣第一,然後添加到像數組:

Antenna = (ImageButton) findViewById(R.id.antenna); 
... 
... 
public ImageButton[] upControllingButtons = {Antenna, AV, Components, VGA, HDMI, Switch}; 
0
public ImageButton[] upControllingButtons = new ImageButton[7]; 
Antenna = (ImageButton) findViewById(R.id.antenna); 
//Then Do your work 

希望這將有助於

0

你的計算策略是錯誤的做不使用單獨的行來爲每個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 
}