java.lang.ClassCastException:请帮我找错

时间:2014-10-10 18:21:13

标签: android java.lang.class

这里是XML代码 http://i.imgur.com/PnhxAOB.png

叹息我一直试图解决这个问题超过一个小时我不知道最近会发生什么..我在最后一行代码上得到了错误:ph0ne= (EditText) findViewById(R.id.testphone);

public class MyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);

        btn1= (Button) findViewById(R.id.buttonGoTonumber);
        btn2= (Button)  findViewById(R.id.test3button);
        layHome = (LinearLayout) findViewById(R.id.layHomeddddddd);
        layAddNumer = (LinearLayout) findViewById(R.id.test3LAY);
        ph0ne= (EditText) findViewById(R.id.testphone);
    }
}

编辑:由于某种原因,我无法添加XML代码..这是stackTrace:

 java.lang.RuntimeException: Unable to start activity ComponentInfo{com.spiratessgmail.myapplication/com.spiratessgmail.myapplication.MyActivity}: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)
     Caused by: java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.EditText
            at com.spiratessgmail.myapplication.MyActivity.onCreate(MyActivity.java:33)
            at android.app.Activity.performCreate(Activity.java:5231)
            at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
            at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

android.widget.LinearLayout cannot be cast to android.widget.EditText

表示您应该更改

 ph0ne= (EditText) findViewById(R.id.testphone);

in

ph0ne= (LinearLayout) findViewById(R.id.testphone);

或者如果您的视图应该是EditText,则必须使用LinearLayout

中的id testphone(EditText)更改视图

答案 1 :(得分:0)

我认为您只是在引用设计ID时出错,请检查您的EditText ID,因为正如您在错误日志中看到的那样,此ID R.id.testphone指的是LinearLayout,只需检查XML中edit-text的ID

即可