我正在使用自定義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.
只是USEE com.android.smsapp.MsgTextView,而不是在XML部分的TextView,並確保你的類MsgTextView擴展的TextView – 2012-03-18 16:40:54