Maihan Nijat
Maihan Nijat

Reputation: 9355

java.lang.ClassCastException while inflating layout

I am working on Android Soft Keyboard and applying a custom theme for keys.

I am doing this with following code:

@Override
public View onCreateInputView() {

    // Set custom theme to input view.
    int themeLayout = sharedPreferences.getInt(THEME_KEY, R.layout.input_1);
    mInputView = (LatinKeyboardView) getLayoutInflater().inflate(
            themeLayout, null);
    mInputView.setOnKeyboardActionListener(this);

    // Apply the selected keyboard to the input view.
    setLatinKeyboard(getSelectedSubtype());

    return mInputView;
} 

But I am getting the following error:

java.lang.ClassCastException: at com.xxx.xxx.android.SoftKeyboard.onCreateInputView (SoftKeyboard.java:159) at com.xxx.xxx.android.SoftKeyboard.onStartInput (SoftKeyboard.java:232) at android.inputmethodservice.InputMethodService.doStartInput (InputMethodService.java:2641) at android.inputmethodservice.InputMethodService$InputMethodImpl.startInput (InputMethodService.java:590) at android.inputmethodservice.IInputMethodWrapper.executeMessage (IInputMethodWrapper.java:186) at com.android.internal.os.HandlerCaller$MyHandler.handleMessage (HandlerCaller.java:37) at android.os.Handler.dispatchMessage (Handler.java:102) at android.os.Looper.loop (Looper.java:154) at android.app.ActivityThread.main (ActivityThread.java:6682) at java.lang.reflect.Method.invoke (Native Method) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:1520) at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:1410)

Layout:

<com.sunzala.xxxx.android.LatinKeyboardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/keyboard"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:background="@drawable/kb_bg_1"
    android:keyBackground="@drawable/key_bg_fill_grey"
    android:keyPreviewLayout="@layout/key_preview_layout"
    android:keyPreviewOffset="@dimen/keyPreviewOffset"
    android:keyTextColor="@color/white"
    android:popupLayout="@layout/keyboard_popup_layout" />

This works when I am testing on my device but got the error on a crash log after publishing the App.

Upvotes: 1

Views: 155

Answers (1)

Nabin Bhandari
Nabin Bhandari

Reputation: 16429

Maybe proguard minified your custom view. You can disable proguard by setting minifyEnabled false in release block of your build.gradle file.

If you want to use proguard, add the following lines in proguard-rules.pro to preserve custom views.

-keep public class * extends android.view.View {
    public <init> (android.content.Context );
    public <init> (android.content.Context , android.util.AttributeSet );
    public <init> (android.content.Context , android.util.AttributeSet , int);
    public void set *(...);
}

Upvotes: 1

Related Questions