【问题标题】:java.lang.ClassCastException while inflating layoutjava.lang.ClassCastException 同时膨胀布局
【发布时间】:2017-09-27 10:12:18
【问题描述】:

我正在开发 Android 软键盘并为按键应用自定义主题。

我正在使用以下代码进行此操作:

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

但我收到以下错误:

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

布局:

<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" />

当我在我的设备上进行测试但在发布应用程序后在崩溃日志中出现错误时,此方法有效。

【问题讨论】:

    标签: java android android-softkeyboard


    【解决方案1】:

    也许 proguard 缩小了您的自定义视图。您可以通过在 build.gradle 文件的发布块中设置 minifyEnabled false 来禁用 proguard。

    如果您想使用 proguard,请在 proguard-rules.pro 中添加以下行以保留自定义视图。

    -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 *(...);
    }
    

    【讨论】:

    • 在 gradle 中未启用缩小的 proguard。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-10-21
    • 1970-01-01
    相关资源
    最近更新 更多