dhali
dhali

Reputation: 386

get the ID of the clicked image button

I have many image buttons on my application.

Currently I use the .getID() for another method for my game. I would also like to see the Id myself.

I have tried to assign the Id to a text view within the button click like this,

clicker = new OnClickListener(){

            @Override
            public void onClick(View v){

                ImageButton clicked = (ImageButton) v;
                makeMove(clicked.getId()); // getting the id of where the person has clicked, and calling makeMove method which is defined below

                TextView clickID = (TextView) findViewById(R.id.clickId);
                clickID.setVisibility(View.VISIBLE);
                clickID.setText(clicked.getId());

            }

        };

But I get a nullpointer for the,

clicked.getId();

Is there another way to get this information?

Also am I assigning it wrong?

I'm very new to android, and don't know where to turn for this. Any help would be much appreciated.

Thanks.

Also my log cat is,

02-12 18:23:27.379: E/AndroidRuntime(1042): FATAL EXCEPTION: main
02-12 18:23:27.379: E/AndroidRuntime(1042): java.lang.NullPointerException
02-12 18:23:27.379: E/AndroidRuntime(1042):     at         com.example.con4.ConnectFourActivity1$2.onClick(ConnectFourActivity1.java:178)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at android.view.View.performClick(View.java:4240)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at         android.view.View$PerformClick.run(View.java:17721)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at android.os.Handler.handleCallback(Handler.java:730)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at android.os.Handler.dispatchMessage(Handler.java:92)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at android.os.Looper.loop(Looper.java:137)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at android.app.ActivityThread.main(ActivityThread.java:5103)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at java.lang.reflect.Method.invokeNative(Native Method)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at java.lang.reflect.Method.invoke(Method.java:525)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-12 18:23:27.379: E/AndroidRuntime(1042):     at dalvik.system.NativeStart.main(Native Method)

Upvotes: 0

Views: 2451

Answers (2)

dhali
dhali

Reputation: 386

I put the the click.getId into method, then displayed it, so then I could view the id in logcat

Upvotes: 1

Shobhit Puri
Shobhit Puri

Reputation: 26007

You can do it in few ways. If you have separate listener for each button, then you know the button and can get it using v.getId(). If you have one listener for all, then you can use switch statements to identify which one was clicked. See Android OnClickListener - identify a button . It explains for a button but its same for ImageButton as well.

Upvotes: 0

Related Questions