My app works greatly with the exception of a few devices. On one such device, I get a FATAL EXCEPTION in one of my activities. The error is java.lang.ClassCastException: java.lang.String cannot be cast to android.text.Spannable ... ... at android.widget.TextView.setEnabled(TextView.java:1432)
STACK TRACE
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)
Here are the snippets of code surrounding the error.
XML LAYOUT CODE
<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" />
CUSTOM BUTTON JAVA CODE
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 CODE CAUSING ERROR
private StyledButton mSignIn;
public void checkIfAnyFieldIsEmpty() {
if (mEmailEmpty || mPasswordEmpty) {
mSignIn.setEnabled(false);
} else {
mSignIn.setEnabled(true);
}
}
The line of code mSignIn.setEnabled(false) is causing the error. If i comment it out or just replace it so both the if and else read mSignIn.setEnabled(true) the app runs fine.
Again. The code as is works fine on the majority of devices.
setEnabled()
references anyString
orSpannable
. You are probably tripping over a bug in some manufacturer-specific modification to Android. You might wish to try to contact the manufacturer in question. – MontespanAnyTextView
(github.com/hanspeide/anytextview), which has a similar objective as yourStyledButton
, to see how it handles your typeface manipulations, and see if perhaps it survives better on these devices. "will contacting them actually help?" -- that would depend a bit on the manufacturer, which you have not yet disclosed. – Montespan