我正在做的实际上是一个示例程序。我已经删除了一些无用的代码,以便你们可以清楚地看到这里发生了什么。
public class CreateMyCardFragment extends FragmentActivity implements EditTextListener
{
private EditTextWithTextListener cardPositionEdit = null;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_create_my_card);
cardPositionEdit = (EditTextWithTextListener) findViewById(R.id.cardPositionEdit);
cardPositionEdit.setOnEditTextListener(this);
}
@Override
public void textChanged(int id)
{
switch (id)
{
case R.id.cardPositionEdit:
newCard.setCardPositionShow(cardPositionEdit.getText().toString());
break;
default:
break;
}
}
}
当我的程序进入这个页面时,它只是因为以下错误而崩溃(log)</p>
06-24 23:51:24.710: E/AndroidRuntime(6285): FATAL EXCEPTION: main
06-24 23:51:24.710: E/AndroidRuntime(6285): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.businesscard/com.businesscard.ui.CreateMyCardFragment}: java.lang.ClassCastException: android.widget.EditText cannot be cast to com.businesscard.View.EditTextWithTextListener
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2080)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2105)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread.access$600(ActivityThread.java:136)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1201)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.os.Handler.dispatchMessage(Handler.java:99)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.os.Looper.loop(Looper.java:137)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread.main(ActivityThread.java:4797)
06-24 23:51:24.710: E/AndroidRuntime(6285): at java.lang.reflect.Method.invokeNative(Native Method)
06-24 23:51:24.710: E/AndroidRuntime(6285): at java.lang.reflect.Method.invoke(Method.java:511)
06-24 23:51:24.710: E/AndroidRuntime(6285): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-24 23:51:24.710: E/AndroidRuntime(6285): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-24 23:51:24.710: E/AndroidRuntime(6285): at dalvik.system.NativeStart.main(Native Method)
06-24 23:51:24.710: E/AndroidRuntime(6285): Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to com.businesscard.View.EditTextWithTextListener
06-24 23:51:24.710: E/AndroidRuntime(6285): at com.businesscard.ui.CreateMyCardFragment.onCreate(CreateMyCardFragment.java:76)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.Activity.performCreate(Activity.java:5065)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
06-24 23:51:24.710: E/AndroidRuntime(6285): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2044)
06-24 23:51:24.710: E/AndroidRuntime(6285): ... 11 more
06-24 23:54:26.445: E/Trace(6853): error opening trace file: No such file or directory (2)
有谁知道发生了什么?
EditTextWithTextListener 类是这样定义的
public class EditTextWithTextListener extends EditText
{
private EditTextListener myTextListener = null;
public EditTextWithTextListener(Context context) {
super(context);
}
public EditTextWithTextListener(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
public EditTextWithTextListener(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public void setOnEditTextListener(EditTextListener listener)
{
this.myTextListener = listener;
}
@Override
protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter)
{
if (null != myTextListener)
myTextListener.textChanged(this.getId());
super.onTextChanged(text, start, lengthBefore, lengthAfter);
}
}
有谁知道这里发生了什么?为什么不能将 EditText 转换为它的子类?欢迎任何帮助,在此先感谢。