迁移到Android X应用后,崩溃了android.view.InflateException

时间:2019-03-18 11:42:21

标签: android androidx

将应用迁移到Android X应用后,运行时崩溃。当“片段的活动”使布局膨胀时会发生这种情况。我的StackTrace是:

  

进程:com.sizestream.android,PID:17380       java.lang.RuntimeException:无法恢复活动{package / ... Activity}:android.view.InflateException:二进制XML文件第50行:二进制XML文件第50行:膨胀类错误           在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3581)           在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6494)           在java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)        原因:android.view.InflateException:二进制XML文件第50行:二进制XML文件第50行:膨胀类错误        原因:android.view.InflateException:二进制XML文件行#50:错误夸大类        造成原因:java.lang.reflect.InvocationTargetException           在java.lang.reflect.Constructor.newInstance0(本地方法)           在java.lang.reflect.Constructor.newInstance(Constructor.java:334)           在android.view.LayoutInflater.createView(LayoutInflater.java:647)           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:790)           在android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:730)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:863)           在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)           在android.view.LayoutInflater.rInflate(LayoutInflater.java:866)           在android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:824)           在android.view.LayoutInflater.inflate(LayoutInflater.java:515)           在android.view.LayoutInflater.inflate(LayoutInflater.java:423)           在androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:126)           在androidx.databinding.DataBindingUtil.inflate(DataBindingUtil.java:95)           在com.common.mvvm.BaseFragment.onCreateView(BaseFragment.kt:43)           在androidx.fragment.app.Fragment.performCreateView(Fragment.java:2530)           在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:887)           在androidx.fragment.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManagerImpl.java:1233)           在androidx.fragment.app.FragmentManagerImpl.moveToState(FragmentManagerImpl.java:1299)           在androidx.fragment.app.BackStackRecord.executeOps(BackStackRecord.java:688)           在androidx.fragment.app.FragmentManagerImpl.executeOps(FragmentManagerImpl.java:2069)           在androidx.fragment.app.FragmentManagerImpl.executeOpsTogether(FragmentManagerImpl.java:1859)           在androidx.fragment.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManagerImpl.java:1814)           在androidx.fragment.app.FragmentManagerImpl.execPendingActions(FragmentManagerImpl.java:1721)           在androidx.fragment.app.FragmentController.execPendingActions(FragmentController.java:447)           在androidx.fragment.app.FragmentActivity.onResume(FragmentActivity.java:444)           在android.app.Instrumentation.callActivityOnResume(Instrumentation.java:1355)           在android.app.Activity.performResume(Activity.java:7117)           在android.app.ActivityThread.performResumeActivity(ActivityThread.java:3556)           在android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3621)           在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2862)           在android.app.ActivityThread.-wrap11(未知来源:0)           在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1589)           在android.os.Handler.dispatchMessage(Handler.java:106)           在android.os.Looper.loop(Looper.java:164)           在android.app.ActivityThread.main(ActivityThread.java:6494)   2019-03-18 13:05:21.536 17380-17380 / com.sizestream.android E / AndroidRuntime:at java.lang.reflect.Method.invoke(本机方法)           在com.android.internal.os.RuntimeInit $ MethodAndArgsCaller.run(RuntimeInit.java:438)           在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)

1 个答案:

答案 0 :(得分:3)

经过一些调试后,我发现在迁移之前项目使用material-components-android时会发生问题,并且某些操作必须手动完成:

  1. TextInputLayout 子视图应为 com.google.android.material.textfield.TextInputEditText 而不是EditText或AppCompatEditText:

    <com.google.android.material.textfield.TextInputLayout
                android:id="@+id/input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">
    
                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
    

  2. TextInputLayout 应该使用从 Widget.MaterialComponents.TextInputLayout.FilledBox <继承的 android:theme app:errorTextAppearance / strong>

styles.xml:

<style name="TextInputLayoutStyle" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    ...
</style>
<style name="TextInputLayoutErrorAppearance" parent="Widget.MaterialComponents.TextInputLayout.FilledBox">
    ...
</style>

layout.xml

<com.google.android.material.textfield.TextInputLayout
                android:id="@+id/input_layout"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:theme="@style/TextInputLayoutStyle"
                app:errorEnabled="true"
                app:errorTextAppearance="@style/TextInputLayoutErrorAppearance">

                <com.google.android.material.textfield.TextInputEditText
                    android:id="@+id/edit_text"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"/>
</com.google.android.material.textfield.TextInputLayout>
  1. 如果要切换密码输入类型,则必须将 app:passwordToggleEnabled app:passwordToggleTint 替换为 app:endIconMode =“ password_toggle” < / strong>

    <com.google.android.material.textfield.TextInputLayout
                    android:id="@+id/password_input_layout"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:errorEnabled="true"
                    app:endIconMode="password_toggle">
    
    
                <com.google.android.material.textfield.TextInputEditText
                        android:id="@+id/password_edit_text"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:inputType="textPassword"/>