ClassCastException:android.widget.EditText无法强制转换为Models.EditTextMonitor

时间:2014-10-17 11:17:07

标签: java android android-edittext classcastexception

我在Models包中有类EditTextMonitor,例如:

public class EditTextMonitor extends EditText{
private final Context mcontext; // Just the constructors to create a new EditText...

public EditTextMonitor(Context context) {
    super(context);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs) {
    super(context, attrs);
    this.mcontext = context;
}

public EditTextMonitor(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    this.mcontext = context;
}


@Override
public boolean onTextContextMenuItem(int id) {
    // Do your thing:
    boolean consumed = super.onTextContextMenuItem(id);
    // React:
    switch (id){
        case android.R.id.copy:
            onTextCopy();
        case android.R.id.paste:
            onTextCopy();
    }
    return consumed;
}
public void onTextCopy(){
    ((TextEditorActivity)mcontext).startAmode();
}
}

在我的java类中:EditTextMonitor editor =(EditTextMonitor)findViewById(R.id.texteditor);

在xml视图文件中定义:

<Models.EditTextMonitor
        android:id="@+id/texteditor"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="center_horizontal"
        android:layout_margin="5dp"
        android:background="@color/white"
        android:ems="10"
        android:gravity="top"
        android:inputType="textMultiLine"
        android:padding="5dp"
        android:textAllCaps="false" />

但我有错误:

java.lang.RuntimeException: Unable to start activity 

ComponentInfo{com.atompark.atompark_sms/com.atompark.atompark_sms.TextEditorActivity}: java.lang.ClassCastException: android.widget.EditText cannot be cast to Models.EditTextMonitor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2413)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2471)
at android.app.ActivityThread.access$900(ActivityThread.java:175)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1308)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:146)
at android.app.ActivityThread.main(ActivityThread.java:5603)
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:1283)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.ClassCastException: android.widget.EditText cannot be cast to Models.EditTextMonitor
at com.atompark.atompark_sms.TextEditorActivity.prepereInterface(TextEditorActivity.java:194)
at com.atompark.atompark_sms.TextEditorActivity.onCreate(TextEditorActivity.java:97)
... 

我只在平板电脑设备上出现此错误。

1 个答案:

答案 0 :(得分:0)

在xml中使用具有正确包名的类名和TextEditorActivity

com.atompark.atompark_sms.TextEditorActivity