2014-10-10 62 views
0

確定這裏是XML代碼http://i.imgur.com/PnhxAOB.pngjava.lang.ClassCastException:幫我看看什麼是錯,請

嗟我一直在嘗試怎麼回事解決這一問題進行過小時,我不知道。我得到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代碼。這裏是堆棧跟蹤:

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) 
+0

提供堆棧跟蹤爲好。 – Ambrish 2014-10-10 18:23:30

+0

Iam對不起Iam新到這,什麼是堆棧跟蹤?爲什麼不能發送XML代碼.. – jojo 2014-10-10 18:24:52

+1

我們需要堆棧跟蹤(LogCat中顯示的紅色錯誤日誌),但是我猜測R.id.testphone的類型不是'EditText'。無論是那個還是'ph0ne'都有錯誤的類型。你能發佈更多的代碼嗎? – aProperFox 2014-10-10 18:25:00

回答

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

意味着你應該在

ph0ne= (LinearLayout) findViewById(R.id.testphone)改變

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

;

,或者如果您的視圖應該是EditText,你必須改變在EditText

+0

那是行不通的。我得到紅色下劃線,如果我改變它,它說它需要EditText,但發現LinearLayout – jojo 2014-10-10 18:32:44

+0

然後問題是'ph0ne'有一種LinearLayout。請將其更改爲「TextView ph0ne;」在初始化中。 – aProperFox 2014-10-10 18:33:44

+0

@jojo可能來自您的結束標記。如果你把一個你的密碼標籤必須是。或者你可以刪除結束標籤,並使用第一個標籤在最後的角度支架前添加一個斜槓 – GVillani82 2014-10-10 18:35:32

0

我覺得你只是犯了一個錯誤上關於你的設計IDS與ID testphone的觀點(這是一個LinearLayout),請檢查你的EditText的ID,因爲當你在你的錯誤日誌中看到的,此ID R.id.testphoneLinearLayout,請查看您的edit-text的ID在你的XML

+0

不是:(。<編輯文本 android:layout_width =「match_parent」 android:layout_height =「wrap_content」 android:inputType =「phone」 android:ems =「10」 android:id =「@ + id/testphone」/> – jojo 2014-10-10 18:42:20