1

我的 xml 布局中有一个导致崩溃的按钮。我已经能够通过将android:bufferType="spannable"添加到布局文件或删除行android:imeOptions="actionDone"来防止崩溃。我很高兴我能够弄清楚,但我不明白为什么其中任何一个都可以作为解决方案。有人可以帮忙解释一下吗?我在初始堆栈跟踪下方包含了来自 xml 布局的代码、我的自定义按钮类的 java 代码,以及根据堆栈跟踪导致错误的代码行。但就像我之前提到的,我可以通过添加特定的代码行或删除特定的行来修复崩溃。我在这里先向您的帮助表示感谢。

堆栈跟踪

05-02 09:18:19.917: E/AndroidRuntime(20587): FATAL EXCEPTION: main
05-02 09:18:19.917: E/AndroidRuntime(20587): java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spannable
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.widget.TextView.setEnabled(TextView.java:1432)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at com.myapp.android.menu.LoginFragment.checkIfAnyFieldIsEmpty(LoginFragment.java:512)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at com.myapp.android.menu.LoginFragment.onCreateView(LoginFragment.java:183)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.Fragment.performCreateView(Fragment.java:1460)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:911)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1088)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:682)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.os.Handler.handleCallback(Handler.java:615)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.os.Handler.dispatchMessage(Handler.java:92)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.os.Looper.loop(Looper.java:137)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at android.app.ActivityThread.main(ActivityThread.java:4962)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at java.lang.reflect.Method.invokeNative(Native Method)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at java.lang.reflect.Method.invoke(Method.java:511)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1004)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:771)
05-02 09:18:19.917: E/AndroidRuntime(20587):    at dalvik.system.NativeStart.main(Native Method)

以下是围绕错误的代码片段。

XML 布局代码

<com.myapp.android.view.StyledButton
        android:id="@+id/login_sign_in"
        style="@style/button_blue"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:background="@drawable/state_list_login"
        android:textColor="@color/white"
        android:imeOptions="actionDone"
        android:padding="10dp"
        android:text="@string/sign_in" />

自定义按钮 JAVA 代码

    public class StyledButton extends Button {
    //variables
    private final String BOLD = "fonts/arial.ttf";
    private final String NORMAL = "fonts/arial_bold.ttf";

    private String mCustomFont = null;

    /**
     * Constructor
     * @param context
     */
    public StyledButton(Context context) {
        super(context);
    }

    /**
     * Constructor
     * @param context
     * @param attrs
     */
    public StyledButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        setCustomAttributes(attrs);
    }

    /**
     * Set custom attributes
     * @param attrs
     */
    private void setCustomAttributes(AttributeSet attrs) {
        TypedArray a=getContext().obtainStyledAttributes(attrs,R.styleable.StyledTextView);
        mCustomFont = a.getString(R.styleable.StyledTextView_fontAsset);
        if(mCustomFont != null) {
            if(!isInEditMode()) {
                CustomFontManager fontManager = CustomFontManager.getInstance();
                super.setTypeface(fontManager.getFont(getContext().getAssets(), mCustomFont));
            }
        }
        a.recycle();
    }

    /**
     * Set type face
     */
    public void setTypeface(Typeface tf, int style) {
        CustomFontManager fontManager = CustomFontManager.getInstance();
        if(isInEditMode()) {
            super.setTypeface(tf,style);
            return;
        }
        if(mCustomFont!=null) {
            super.setTypeface(fontManager.getFont(getContext().getAssets(), mCustomFont));
            return;
        }
        switch (style) {
            case Typeface.NORMAL:
                super.setTypeface(fontManager.getFont(getContext().getAssets(), NORMAL));
                break;
            case Typeface.BOLD:
                super.setTypeface(fontManager.getFont(getContext().getAssets(), BOLD));
                break;
            default:
                super.setTypeface(fontManager.getFont(getContext().getAssets(), NORMAL));
                break;
        }
    }
}

JAVA代码导致错误

    private StyledButton mSignIn;

    public void checkIfAnyFieldIsEmpty() {
    if (mEmailEmpty || mPasswordEmpty) {
        mSignIn.setEnabled(false);
    } else {
        mSignIn.setEnabled(true);
    } 
}
4

0 回答 0