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
The Dialog class does not extend Context, which is required for the
onClickattribute.From the documentation
You can do this:
And reference it later on:
If this helps you organize your code better (I like it, along with Orabig’s suggestion).