android.widget.EditText无法强制转换为com.businesscard.View.EditTextWithTextListener

时间:2013-06-24 16:28:00

标签: android

我正在研究的是一个示例程序。我删除了一些无用的代码,以便你们可以清楚地看到这里发生了什么。

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;
        }
    }
}

当我的程序进入此页面时,它刚刚崩溃并出现以下错误(日志)

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无法转换为它的子类? 欢迎任何帮助,提前谢谢。

1 个答案:

答案 0 :(得分:1)

创建自定义View后,请确保在XML中定义它。

在您的fragment_create_my_card.xml中,您应该像这样引用您的观点:

<com.businesscard.View.EditTextWithTextListener
    android:id="@+id/cardPositionEdit"
    android:layout_height="wrap_content"
    android:layout_weight="wrap_content" />

EditText无法强制转换为其子类,因为它对此子类一无所知。

EditTextWithTextListenerEditTextEditText不是EditTextWithTextListener