2012-03-18 35 views
0

我正在使用自定義TextView。但是,當我使用ID查找視圖來檢索它時,我得到了ClassCastException。我在XML文件中使用這樣的:使用自定義視圖時findViewById中的ClassCastException

<View class="com.android.smsapp.MsgTextView" android:id="@+id/text"

而且在這樣的java文件,使用它。

MsgTextView text = (MsgTextView) row.findViewById(R.id.text);

我在做什麼錯?


@Pavandroid

我已經在正確的文件

package com.android.smsapp; 

    import android.content.Context; 
    import android.util.AttributeSet; 
    import android.widget.TextView; 

    public class MsgTextView extends TextView { 
    private String sender; 

    MsgTextView(Context c){ 
     super(c); 
    } 

    MsgTextView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
    } 

    MsgTextView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
    } 

    public void setSender(String s) { 
     sender = s; 
    } 

    public String getSender() { 
     return (sender); 
    } 
}` 

還包括它當我檢查正常,logcat的是示出一個多個線路

03-19 14:30:41.476: E/AndroidRuntime(24089): Caused by: java.lang.NoSuchMethodException: MsgTextView(Context,AttributeSet).. But I have defined this constructor. 
+1

只是USEE com.android.smsapp.MsgTextView,而不是在XML部分的TextView,並確保你的類MsgTextView擴展的TextView – 2012-03-18 16:40:54

回答

1

代替上述代碼使用下面的代碼。

<com.android.smsapp.MsgTextView 
      android:id="@+id/text" 
      additional parameters here....> 
</com.android.smsapp.MsgTextView> 
+0

試了一下。但現在我得到「03-19 13:03:30.632: E/AndroidRuntime(22775):java.lang.RuntimeException:無法啓動活動ComponentInfo {com.android.smsapp/com.android.smsapp.SMSAppActivity}:android.view.InflateException:二進制XML文件行#22:錯誤膨脹類com.android.smsapp.MsgTextView「 – Sumit 2012-03-19 07:41:56

+0

您是否將MsgTextView放在適當的包中,即com.android.smsapp?請仔細檢查,然後再試一次.... – Pavandroid 2012-03-19 07:44:51

+0

仍在收到問題...一切看起來都很好...已粘貼MsgTextView類 – Sumit 2012-03-19 09:11:57

相關問題