0

I want to hide all image ImageButton on the app launch;

how can I archive that? here's the code:

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

Update I changed the hideBtn method then I got Force Close while testing

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

Crash log cat report:

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

Update : this is the xml code:

<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" />
jk jk
  • 1,027
  • 2
  • 13
  • 28

1 Answers1

1

findViewById needs the argument of type int but you are passing type of string. You need to get the resource id first.

you can use getIdentifier method of resource class to get the id, check this getIdentifier

do like this

int id=getResources().getIdentifier("myBTN"+[i], "id", "com.mypackage.myapp");

then

imgBtn[i] = (ImageButton) findViewById(id);
Pragnani
  • 20,075
  • 6
  • 49
  • 74