运行应用时显示错误

时间:2017-02-11 08:08:17

标签: android-studio

错误如下:

  

02-11 13:24:31.561 5262-5262 /? E / AndroidRuntime:致命异常:主要                                                    处理:com.example.vicky.project,PID:5262                                                    java.lang.RuntimeException:无法启动活动   ComponentInfo {com.example.vicky.project / com.example.vicky.project.MainActivity}:   java.lang.ClassCastException:   android.support.v7.widget.AppCompatTextView无法强制转换为   android.widget.EditText                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2646)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                        在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:154)                                                        在android.app.ActivityThread.main(ActivityThread.java:6077)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)                                                     引起:java.lang.ClassCastException:   android.support.v7.widget.AppCompatTextView无法强制转换为   android.widget.EditText                                                        在com.example.vicky.project.MainActivity.onCreate(MainActivity.java:30)                                                        在android.app.Activity.performCreate(Activity.java:6662)                                                        在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1118)                                                        在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2599)                                                        在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2707)                                                        在android.app.ActivityThread.-wrap12(ActivityThread.java)                                                        在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1460)                                                        在android.os.Handler.dispatchMessage(Handler.java:102)                                                        在android.os.Looper.loop(Looper.java:154)                                                        在android.app.ActivityThread.main(ActivityThread.java:6077)                                                        at java.lang.reflect.Method.invoke(Native Method)                                                        在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:865)                                                        在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

2 个答案:

答案 0 :(得分:0)

类强制转换异常,因为您正在尝试将TextView强制转换为EditText。

答案 1 :(得分:0)

仔细看看 {android.support.v7.widget.AppCompatTextView cannot be cast to android.widget.EditText at com.example.vicky.project.MainActivity.onCreate(MainActivity.java:30)}

你可能会这样做 {TextView tv = (EditText/*change this to TextView*/) findViewById (R.id.textView)} 如果您确定您的代码是正确的,那么在layout.xml中更改视图的ID,然后从构建选项下清除项目并重建它。