本文介绍了了java.lang.RuntimeException:java.lang.ClassCastException:MainActivity不能转换为android.view.View $ OnClickListener的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我是新来的机器人。我有停留在上面的问题。请你帮我解决这个问题。
I am new to android. I have got stuck at the above issue. Please do help me to fix this.
我的主要活动:
import com.db.appform12.R;
import app.form.appform12.ListUsers;
import app.form.appform12.EditUsers;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button regButton = (Button)findViewById(R.id.buttonMainRegister);
Button listButton = (Button)findViewById(R.id.ButtonMainList);
regButton.setOnClickListener((OnClickListener) this);
listButton.setOnClickListener((OnClickListener) this);
}
public void onClick(View v) {
Intent intent;
switch(v.getId()) {
case R.id.buttonMainRegister :
intent = new Intent();
intent.setClass(getApplicationContext(), EditUsers.class);
startActivity(intent);
break;
case R.id.ButtonMainList :
intent = new Intent();
intent.setClass(getApplicationContext(), ListUsers.class);
startActivity(intent);
break;
}
}
}
完整LogCat中跟踪:
Complete LogCat trace:
12-29 06:23:19.448: D/dalvikvm(2001): Not late-enabling CheckJNI (already on)
12-29 06:23:22.018: I/dalvikvm(2001): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-29 06:23:22.018: W/dalvikvm(2001): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-29 06:23:22.018: D/dalvikvm(2001): VFY: replacing opcode 0x6f at 0x0000
12-29 06:23:22.018: I/dalvikvm(2001): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-29 06:23:22.018: W/dalvikvm(2001): VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-29 06:23:22.018: D/dalvikvm(2001): VFY: replacing opcode 0x6f at 0x0000
12-29 06:23:22.068: I/dalvikvm(2001): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-29 06:23:22.068: W/dalvikvm(2001): VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-29 06:23:22.088: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x000e
12-29 06:23:22.378: I/dalvikvm(2001): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-29 06:23:22.378: W/dalvikvm(2001): VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 06:23:22.378: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x0002
12-29 06:23:22.378: I/dalvikvm(2001): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-29 06:23:22.378: W/dalvikvm(2001): VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I
12-29 06:23:22.378: D/dalvikvm(2001): VFY: replacing opcode 0x6e at 0x0002
12-29 06:23:24.038: D/AndroidRuntime(2001): Shutting down VM
12-29 06:23:24.038: W/dalvikvm(2001): threadid=1: thread exiting with uncaught exception (group=0xb0f44648)
12-29 06:23:24.168: E/AndroidRuntime(2001): FATAL EXCEPTION: main
12-29 06:23:24.168: E/AndroidRuntime(2001): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.appform12/app.form.appform12.MainActivity}: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.os.Looper.loop(Looper.java:137)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 06:23:24.168: E/AndroidRuntime(2001): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 06:23:24.168: E/AndroidRuntime(2001): at java.lang.reflect.Method.invoke(Method.java:525)
12-29 06:23:24.168: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 06:23:24.168: E/AndroidRuntime(2001): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 06:23:24.168: E/AndroidRuntime(2001): at dalvik.system.NativeStart.main(Native Method)
12-29 06:23:24.168: E/AndroidRuntime(2001): Caused by: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:23:24.168: E/AndroidRuntime(2001): at app.form.appform12.MainActivity.onCreate(MainActivity.java:28)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.Activity.performCreate(Activity.java:5133)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 06:23:24.168: E/AndroidRuntime(2001): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-29 06:23:24.168: E/AndroidRuntime(2001): ... 11 more
12-29 06:23:48.758: I/Process(2001): Sending signal. PID: 2001 SIG: 9
12-29 06:25:16.559: I/dalvikvm(2061): Could not find method android.view.ViewGroup.onNestedScrollAccepted, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onNestedScrollAccepted
12-29 06:25:16.629: W/dalvikvm(2061): VFY: unable to resolve virtual method 11352: Landroid/view/ViewGroup;.onNestedScrollAccepted (Landroid/view/View;Landroid/view/View;I)V
12-29 06:25:16.629: D/dalvikvm(2061): VFY: replacing opcode 0x6f at 0x0000
12-29 06:25:16.629: I/dalvikvm(2061): Could not find method android.view.ViewGroup.onStopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.onStopNestedScroll
12-29 06:25:16.649: W/dalvikvm(2061): VFY: unable to resolve virtual method 11358: Landroid/view/ViewGroup;.onStopNestedScroll (Landroid/view/View;)V
12-29 06:25:16.649: D/dalvikvm(2061): VFY: replacing opcode 0x6f at 0x0000
12-29 06:25:16.699: I/dalvikvm(2061): Could not find method android.support.v7.internal.widget.ActionBarOverlayLayout.stopNestedScroll, referenced from method android.support.v7.internal.widget.ActionBarOverlayLayout.setHideOnContentScrollEnabled
12-29 06:25:16.719: W/dalvikvm(2061): VFY: unable to resolve virtual method 9045: Landroid/support/v7/internal/widget/ActionBarOverlayLayout;.stopNestedScroll ()V
12-29 06:25:16.719: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x000e
12-29 06:25:16.879: I/dalvikvm(2061): Could not find method android.content.res.TypedArray.getChangingConfigurations, referenced from method android.support.v7.internal.widget.TintTypedArray.getChangingConfigurations
12-29 06:25:16.899: W/dalvikvm(2061): VFY: unable to resolve virtual method 370: Landroid/content/res/TypedArray;.getChangingConfigurations ()I
12-29 06:25:16.899: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x0002
12-29 06:25:16.919: I/dalvikvm(2061): Could not find method android.content.res.TypedArray.getType, referenced from method android.support.v7.internal.widget.TintTypedArray.getType
12-29 06:25:16.939: W/dalvikvm(2061): VFY: unable to resolve virtual method 392: Landroid/content/res/TypedArray;.getType (I)I
12-29 06:25:16.939: D/dalvikvm(2061): VFY: replacing opcode 0x6e at 0x0002
12-29 06:25:17.979: D/AndroidRuntime(2061): Shutting down VM
12-29 06:25:18.019: W/dalvikvm(2061): threadid=1: thread exiting with uncaught exception (group=0xb0f44648)
12-29 06:25:18.019: E/AndroidRuntime(2061): FATAL EXCEPTION: main
12-29 06:25:18.019: E/AndroidRuntime(2061): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.db.appform12/app.form.appform12.MainActivity}: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2211)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2261)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.access$600(ActivityThread.java:141)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1256)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.os.Handler.dispatchMessage(Handler.java:99)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.os.Looper.loop(Looper.java:137)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.main(ActivityThread.java:5103)
12-29 06:25:18.019: E/AndroidRuntime(2061): at java.lang.reflect.Method.invokeNative(Native Method)
12-29 06:25:18.019: E/AndroidRuntime(2061): at java.lang.reflect.Method.invoke(Method.java:525)
12-29 06:25:18.019: E/AndroidRuntime(2061): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
12-29 06:25:18.019: E/AndroidRuntime(2061): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
12-29 06:25:18.019: E/AndroidRuntime(2061): at dalvik.system.NativeStart.main(Native Method)
12-29 06:25:18.019: E/AndroidRuntime(2061): Caused by: java.lang.ClassCastException: app.form.appform12.MainActivity cannot be cast to android.view.View$OnClickListener
12-29 06:25:18.019: E/AndroidRuntime(2061): at app.form.appform12.MainActivity.onCreate(MainActivity.java:25)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.Activity.performCreate(Activity.java:5133)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
12-29 06:25:18.019: E/AndroidRuntime(2061): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2175)
12-29 06:25:18.019: E/AndroidRuntime(2061): ... 11 more
我不知道这些类型的错误。任何建议是值得欢迎的。先谢谢了。
I am not aware of these types of errors. Any suggestion is welcome. Thanks in advance.
推荐答案
您不能设置 OnClickListener
是这样的:
regButton.setOnClickListener((OnClickListener) this);
listButton.setOnClickListener((OnClickListener) this);
相反,你需要实现OnClickListener您的活动:
Instead, you need to implement OnClickListener in your activity:
public class MyActivity extends Activity implements OnClickListener{
//..
//then just set this as OnClickListener. and you would need to implement method onClick.
regButton.setOnClickListener(this);
listButton.setOnClickListener(this);
//..
}
这篇关于了java.lang.RuntimeException:java.lang.ClassCastException:MainActivity不能转换为android.view.View $ OnClickListener的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!