2011-02-16 248 views
5

這是我的代碼:類轉換異常

package com.example.userpage; 

import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 
import android.widget.EditText; 
import android.widget.TextView; 

public class UserPage extends Activity { 

    String tv,tv1; 
    EditText name,pass; 
    TextView x,y; 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.main); 
     Button button = (Button) findViewById(R.id.widget44); 
     button.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       name.setText(" "); 
       pass.setText(" "); 
      } 
     }); 

     x = (TextView) findViewById(R.id.widget46); 
     y = (TextView) findViewById(R.id.widget47); 
     name = (EditText)findViewById(R.id.widget41); 
     pass = (EditText)findViewById(R.id.widget42); 
     Button button1 = (Button) findViewById(R.id.widget45); 
     button1.setOnClickListener(new View.OnClickListener() { 
      public void onClick(View v) { 

       tv= name.getText().toString(); 
       tv1 = pass.getText().toString(); 
       x.setText(tv); 
       y.setText(tv1); 
      } 
     }); 
    } 
} 

這是我的日誌貓:

02-16 12:28:32.648: ERROR/AndroidRuntime(1039): FATAL EXCEPTION: main 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.userpage/com.example.userpage.UserPage}: java.lang.ClassCastException: android.widget.TextView 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread.access$2300(ActivityThread.java:125) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.os.Handler.dispatchMessage(Handler.java:99) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.os.Looper.loop(Looper.java:123) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread.main(ActivityThread.java:4627) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at java.lang.reflect.Method.invokeNative(Native Method) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at java.lang.reflect.Method.invoke(Method.java:521) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at dalvik.system.NativeStart.main(Native Method) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039): Caused by: java.lang.ClassCastException: android.widget.TextView 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at com.example.userpage.UserPage.onCreate(UserPage.java:34) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 
02-16 12:28:32.648: ERROR/AndroidRuntime(1039):  ... 11 more 
02-16 12:28:32.698: WARN/ActivityManager(67): Force finishing activity com.example.userpage/.UserPage 
02-16 12:28:32.967: DEBUG/dalvikvm(292): GC_EXPLICIT freed 46 objects/2240 bytes in 6840ms 
02-16 12:28:33.247: WARN/ActivityManager(67): Activity pause timeout for HistoryRecord{43ee7b70 com.example.userpage/.UserPage} 
02-16 12:28:36.947: INFO/Process(1039): Sending signal. PID: 1039 SIG: 9 
02-16 12:28:37.017: INFO/ActivityManager(67): Process com.example.userpage (pid 1039) has died. 
02-16 12:28:37.128: WARN/InputManagerService(67): Window already focused, ignoring focus gain of: [email protected] 
02-16 12:28:42.087: DEBUG/dalvikvm(176): GC_EXPLICIT freed 156 objects/11488 bytes in 145ms 
02-16 12:28:45.391: WARN/ActivityManager(67): Activity destroy timeout for HistoryRecord{43ee7b70 com.example.userpage/.UserPage} 
02-16 12:28:47.177: DEBUG/SntpClient(67): request time failed: java.net.SocketException: Address family not supported by protocol 
+2

發表您的XML還 – ingsaurabh 2011-02-16 07:07:36

+1

請只發送錯誤日誌。不要把完整的logcat。在這個問題中也把你的佈局代碼。那麼只有我們可以找出錯誤。 – Mathew 2011-02-16 07:07:42

+0

請僅向我們提供logcat中那些出現錯誤而不是整個logcat輸出的部分。不要忘記格式化它。 – 2011-02-16 07:11:09

回答

11

如果

Caused by: java.lang.ClassCastException: android.widget.TextView 02-16 12:28:32.648: ERROR/AndroidRuntime(1039): at com.example.userpage.UserPage.onCreate(UserPage.java:34) 

...在你的代碼,然後看起來好像你試圖將一些(非textview)對象轉換爲TextView。嘗試在onCreate中設置調試點並檢查發生了什麼。

更新
確定R.id.widget46R.id.widget47TextView的還是你不小心創建一個EditText,因爲widget41和widget42是EditTexts。或者是最後兩個TextView,並且您嘗試將它們轉換爲EditTexts。檢查一下。

-1

只是一個TextView添加到您的xml文件

0

也可能是問題,因爲TextView中被轉換爲別的東西。我面臨這個問題,並具有相同的java.lang.ClassCastException。

1

我有同樣的問題。解決方案爲我工作是:

我已經刪除了textview,並通過從圖形佈局中拖動新的textview並用不同的ID命名它來重新創建它。另一個變化是它在xml中的順序。它重新創建後位於xml頁面的底部。

之後,我沒有得到任何類拋出異常。

9

有同樣的問題。我的解決方案:只需清理項目並重建它。出於某種原因,R.class沒有正確更新。

0

刪除R.java 清潔工程 保存文件 構建&運行