我想在应用启动时隐藏所有图像 ImageButton;
我怎样才能存档呢?这是代码:
ImageButton imgBtn[];
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout);
hideBtn();
}
private void hideBtn(){
for (int i = 0; i < 3; i++) {
imgBtn[i] = (ImageButton) findViewById("R.id.myBTN"+[i]);
imgBtn[i].setVisibility(View.GONE);
}
}
更新 我改变了 hideBtn 方法然后我在测试时强制关闭
-159 private void hideBtn(){
-160 for (int i = 0; i < 3; i++) {
-161 int id = getResources().getIdentifier("myBTN"+i,"id", "com.my.app");
-162 imgBtn[i] = (ImageButton)findViewById(id);
-163 imgBtn[i].setVisibility(View.GONE);
-164 }
-165 }
崩溃日志猫报告:
03-18 16:46:05.125: E/AndroidRuntime(19463): at dalvik.system.NativeStart.main(Native Method)
03-18 16:46:05.125: E/AndroidRuntime(19463): Caused by: java.lang.NullPointerException
03-18 16:46:05.125: E/AndroidRuntime(19463): at com.my.app.Main.hideBtn(Main.java:162)
03-18 16:46:05.125: E/AndroidRuntime(19463): at com.my.app.Main.onCreate(Main.java:61)
03-18 16:46:05.125: E/AndroidRuntime(19463): at android.app.Activity.performCreate(Activity.java:5108)
03-18 16:46:05.125: E/AndroidRuntime(19463): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
03-18 16:46:05.125: E/AndroidRuntime(19463): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-18 16:46:05.125: E/AndroidRuntime(19463): ... 11 more
更新:这是 xml 代码:
<ImageButton
android:id="@+id/myBTN1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:background="@drawable/button" />
<ImageButton
android:id="@+id/myBTN2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myBTN1"
android:layout_below="@+id/myBTN1"
android:layout_marginTop="12dp"
android:background="@drawable/button" />
<ImageButton
android:id="@+id/myBTN3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/myBTN2"
android:layout_below="@+id/myBTN2"
android:layout_marginTop="12dp"
android:background="@drawable/button" />