I’m making a very simple application which has to perform an action when the user clicks a button .I tried to do this with a OnClickListener but this is causing the program to stop during runtime . I have looked for different solutions but nothing could help me .
I found that the problem is situated at the line where I placed : Button.setOnClickListener
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(0x7f030000);
Button b = (Button) findViewById (0x7f05000a) ;
b.setOnClickListener((android.view.View.OnClickListener) buttonL) ;
}
private OnClickListener buttonL = new OnClickListener ( ) {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//perform action}
};
The error given by Logcat :
02-04 19:45:46.101: E/AndroidRuntime(27043): FATAL EXCEPTION: main
02-04 19:45:46.101: E/AndroidRuntime(27043): java.lang.RuntimeException: Unable to start activity ComponentInfo{me.sms.smsReminder/me.sms.smsReminder.SmsReminderActivity}: java.lang.ClassCastException: me.sms.smsReminder.SmsReminderActivity$1
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1830)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1851)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.access$1500(ActivityThread.java:132)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1038)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.os.Handler.dispatchMessage(Handler.java:99)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.os.Looper.loop(Looper.java:150)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.main(ActivityThread.java:4277)
02-04 19:45:46.101: E/AndroidRuntime(27043): at java.lang.reflect.Method.invokeNative(Native Method)
02-04 19:45:46.101: E/AndroidRuntime(27043): at java.lang.reflect.Method.invoke(Method.java:507)
02-04 19:45:46.101: E/AndroidRuntime(27043): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
02-04 19:45:46.101: E/AndroidRuntime(27043): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
02-04 19:45:46.101: E/AndroidRuntime(27043): at dalvik.system.NativeStart.main(Native Method)
02-04 19:45:46.101: E/AndroidRuntime(27043): Caused by: java.lang.ClassCastException: me.sms.smsReminder.SmsReminderActivity$1
02-04 19:45:46.101: E/AndroidRuntime(27043): at me.sms.smsReminder.SmsReminderActivity.onCreate(SmsReminderActivity.java:28)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1072)
02-04 19:45:46.101: E/AndroidRuntime(27043): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1794)
02-04 19:45:46.101: E/AndroidRuntime(27043): ... 11 more
Thanks , Tom
You have imported and are using the wrong listener, DialogInterface.OnClickListener. And so there’s an error as your activity can’t be cast into DialogInterface when the listener is triggered. Use View.OnClickListener and it should work.