cannot be cast to android.view.View$OnClickListener in android

  • Post comments:0 Comments

ClassCastException cannot be cast to android.view.View$OnClickListener
In this article, we solve ‘android.view.View$OnClickListener’ error in android studio.
When you run the app and click the button, the app crashes and gives an error says “cannot cast to android.view.”View$OnClickListener’.

ClassCastException cannot be cast

What does android studio logcat say?

Process: com.example.myapplication, PID: 22781
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.BinaryCalculalor}: java.lang.ClassCastException: com.example.myapplication.BinaryCalculalor cannot be cast to android.view.View$OnClickListener
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3449)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:223)
at android.app.ActivityThread.main(ActivityThread.java:7656)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947)
Caused by: java.lang.ClassCastException: com.example.myapplication.BinaryCalculalor cannot be cast to android.view.View$OnClickListener
at com.example.myapplication.BinaryCalculalor.onCreate(BinaryCalculalor.java:22)
at android.app.Activity.performCreate(Activity.java:8000)
at android.app.Activity.performCreate(Activity.java:7984)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1309)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3422)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3601) 
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:85) 
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135) 
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95) 
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2066) 
at android.os.Handler.dispatchMessage(Handler.java:106) 
at android.os.Looper.loop(Looper.java:223) 
at android.app.ActivityThread.main(ActivityThread.java:7656) 
at java.lang.reflect.Method.invoke(Native Method) 
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:592) 
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:947) 

It means this error comes due to ClassCastException.
ClassCastException in simple language, Class cast incorrectly to pass from one activity to another.

How to solve ClassCastException type of error in android studio?

If you have used onclick listener method and don’t implement View.OnClickListener like
Implements View.OnClickListener.

If you used more than one onClick, then you need to implement an abstract method in the OnClickListener.’
firstly, check implements View.OnClickListener{applied or not if you used variable.setOnClickListener((View.OnClickListener) this);

If the above doesn’t seem to be the problem, check the variable. setOnClickListener((View. OnClickListener) this); start properly or not. Sometimes variable.setOnClickListener((View.OnClickListener) this); If it is not initialized properly, then the error also comes.

In the third way, whether you have set the ID correctly or not. So check the ID that is applied properly or not.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments