除了少数设备外,我的应用程序运行良好。在一个这样的设备上,我在我的一项活动中遇到了一个致命的异常。错误是java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spannable ... ... at android.widget.TextView.setEnabled(TextView.java:1432)
堆栈跟踪
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);
}
}
代码行mSignIn.setEnabled(false)导致错误。如果我将其注释掉或只是替换它,以便 if 和 else 都读取 mSignIn.setEnabled(true) 应用程序运行正常。
再次。代码在大多数设备上都可以正常工作。