我手動聲明的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)
@Raghunandan如果你正確地閱讀代碼(和他的評論),他使用'findViewById'在所有6,所以它們全部初始化。它不清楚,但由於他隱藏了代碼。 – Doomsknight
您是否爲您的活動設置了contentView? –
發佈完整的代碼。它不清楚你在做什麼 – Raghunandan