Search code examples
androidjava.lang.class

java.lang.ClassCastException: Help me find whats wrong please


OK HERE IS XML CODE https://i.sstatic.net/b8Ue5.png

Sigh I have been trying to fix this for over an hour i have no idea whats going on.. I get the error on the last row of code: 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);
    }
}

edit: for some reason I can't add the XML code.. Here is 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)

Solution

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

    means that you should change

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

    in

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

    or if your View should be a EditText, you have to change the view with id testphone (that is a LinearLayout) in a EditText