我在android中的应用程序一直在崩溃

时间:2016-11-30 13:16:05

标签: android

我继续在logcat中收到此错误但不知道原因。我似乎也没有任何代码错误。任何帮助将不胜感激。

E/AndroidRuntime: FATAL EXCEPTION: main
                  Process: com.example.admin.trainyourmath, PID: 26931
                  java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.admin.trainyourmath/com.example.admin.trainyourmath.MainActivity}: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2434)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494)
                      at android.app.ActivityThread.access$900(ActivityThread.java:157)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:148)
                      at android.app.ActivityThread.main(ActivityThread.java:5551)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620)
                   Caused by: java.lang.ClassCastException: com.example.admin.trainyourmath.MainActivity cannot be cast to android.view.View$OnClickListener
                      at com.example.admin.trainyourmath.MainActivity.onCreate(MainActivity.java:31)
                      at android.app.Activity.performCreate(Activity.java:6272)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2387)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2494) 
                      at android.app.ActivityThread.access$900(ActivityThread.java:157) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1356) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:148) 
                      at android.app.ActivityThread.main(ActivityThread.java:5551) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:730) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:620) 

1 个答案:

答案 0 :(得分:0)

您正在使用MainActivity作为未正确实现的OnClickListener。将implements View.OnClickListener添加到MainActivity类声明的末尾(如果它尚未存在)。应该看起来像:

public class MainActivity extends AppCompatActivity implements View.OnClickListener

您还需要实现View.OnClickListener的所有方法,以便MainActivity类添加:

@Override
public void onClick(View v) {
    // Do stuff
}