tomdelahaba
tomdelahaba

Reputation: 1048

Custom dialog - only anonymous listeners work

i have a "small" problem with writing my application.. I have custom dialog what is show after click on button. On this custom dialog i have some textviews and buttons.. i want the following: if i click on button in the dialog, application will start call (its not the point of problem, it would does whatever).

But problem is, that i am not able to write the listener. All the time if i write the listener in xml layout file, i get crash of my application. There is one solution - write the listener like a anonymous listener. But i dont like anonym listeners so much (if you have 100buttons in layout and for all buttons you need to add anonym listener the code will be pretty confused).

i am not sure if i need to write View.OnClickListener() or DialogInterface.OnClickListener() - i think that the first one but i am not sure. Anonym listener works with view one, non anonym (over the xml layout file) doesnt work the one and the seconds as well.. can anyone say me "why"??

codes:

this work:

    b.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            Log.e("err","I am here");   
        }
    });

this doesnt work:

-xml_layout_file

<Button 
    android:id="@+id/btn_startCall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:onClick="onClick_startCall"
    android:text="@string/btn_startCall" /> 

-myDialog.java (extends Dialog)

public void onClick_startCall(View v){
    Log.e("err","I am here");       
}

output from log:

08-31 22:08:05.469: W/dalvikvm(9456): threadid=1: thread exiting with uncaught exception (group=0x40a3b1f8)
08-31 22:08:05.475: E/AndroidRuntime(9456): FATAL EXCEPTION: main
08-31 22:08:05.475: E/AndroidRuntime(9456): java.lang.IllegalStateException: Could not find a method onClick_startCall(View) in the activity class android.view.ContextThemeWrapper for onClick handler on view class android.widget.Button with id 'btn_startCall'
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.view.View$1.onClick(View.java:3031)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.view.View.performClick(View.java:3511)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.view.View$PerformClick.run(View.java:14105)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.os.Handler.handleCallback(Handler.java:605)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.os.Handler.dispatchMessage(Handler.java:92)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.os.Looper.loop(Looper.java:137)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.app.ActivityThread.main(ActivityThread.java:4673)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at java.lang.reflect.Method.invokeNative(Native Method)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at java.lang.reflect.Method.invoke(Method.java:511)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at dalvik.system.NativeStart.main(Native Method)
08-31 22:08:05.475: E/AndroidRuntime(9456): Caused by: java.lang.NoSuchMethodException: onClick_startCall [class android.view.View]
08-31 22:08:05.475: E/AndroidRuntime(9456):     at java.lang.Class.getConstructorOrMethod(Class.java:460)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at java.lang.Class.getMethod(Class.java:915)
08-31 22:08:05.475: E/AndroidRuntime(9456):     at android.view.View$1.onClick(View.java:3024)
08-31 22:08:05.475: E/AndroidRuntime(9456):     ... 11 more

Upvotes: 2

Views: 868

Answers (2)

Sam
Sam

Reputation: 86948

The Dialog class does not extend Context, which is required for the onClick attribute.

From the documentation

This name must correspond to a public method that takes exactly one parameter of type View. For instance, if you specify android:onClick="sayHello", you must declare a public void sayHello(View v) method of your context (typically, your Activity).


You can do this:

OnClickListener startButtonListener = new View.OnClickListener() {
    public void onClick(View v) {
        Log.e("err","I am here");   
    }
});

And reference it later on:

b.setOnClickListener(startButtonListener);

If this helps you organize your code better (I like it, along with Orabig's suggestion).

Upvotes: 2

Orab&#238;g
Orab&#238;g

Reputation: 12012

Usually, I prefer to have one listener, linked to all the buttons

// in onCreate() method
MyListener buttonListener = new MyListener();
button1.setOnClickListener(buttonListener);
button2.setOnClickListener(buttonListener);
button3.setOnClickListener(buttonListener);

// definition of my listener (inner or simple class)
public class MyListener implements View.OnClickListener {

    public void onClick(View v) {
        switch (v.getId()) {
           case R.id.button1 :
             // Do this for button 1
             break;

           case R.id.button2 :
             // Do this for button 2
             break;

           case R.id.button3 :
             // Do this for button 3
             break;

        } 
    }
}

Upvotes: 2

Related Questions