如何修复API版本23的androidx中的FloatingActionButton膨胀错误?

时间:2019-07-06 13:43:11

标签: java android floating-action-button androidx

我正在使用androidx创建基本目录应用。使用FloatingActionButton添加条目。 该应用程序可以在模拟器上完美运行,但是当在我的手机上运行时,它会显示android.view.InflateException。

错误-

None.append

FloatingActionButton XML(此代码来自错误的第26行。)-

swal.fire({
    // title: "Search",
    type: "question",
    html: '<form id="searchForm" method="post" action="/search">' +
        "<input type='hidden' />" +
        '<input autofocus class="form-control" type="text" name="search_box" placeholder="Seach..."></form>',
    confirmButtonText: "Search",
    showLoaderOnConfirm: true,
    preConfirm: function (value) {
        document.getElementById("searchForm").submit();
        return new Promise(resolve => {
        })
    },
});

MainActivity代码-

E/AndroidRuntime: FATAL EXCEPTION: main
    Process: com.vestige.directory2, PID: 10648
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.vestige.directory2/com.vestige.directory2.Activities.MainActivity}: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2545)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605)
        at android.app.ActivityThread.access$1100(ActivityThread.java:165)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:179)
        at android.app.ActivityThread.main(ActivityThread.java:5730)
        at java.lang.reflect.Method.invoke(Native Method)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681)
     Caused by: android.view.InflateException: Binary XML file line #26: Binary XML file line #26: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
        at android.view.LayoutInflater.inflate(LayoutInflater.java:539)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374)
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469)
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140)
        at com.vestige.directory2.Activities.MainActivity.onCreate(MainActivity.java:51)
        at android.app.Activity.performCreate(Activity.java:6439)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:179) 
        at android.app.ActivityThread.main(ActivityThread.java:5730) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 
     Caused by: android.view.InflateException: Binary XML file line #26: Error inflating class com.google.android.material.floatingactionbutton.FloatingActionButton
        at android.view.LayoutInflater.createView(LayoutInflater.java:645)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835)
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515)
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.vestige.directory2.Activities.MainActivity.onCreate(MainActivity.java:51) 
        at android.app.Activity.performCreate(Activity.java:6439) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:179) 
        at android.app.ActivityThread.main(ActivityThread.java:5730) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 
     Caused by: java.lang.reflect.InvocationTargetException
        at java.lang.reflect.Constructor.newInstance(Native Method)
        at android.view.LayoutInflater.createView(LayoutInflater.java:619)
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.vestige.directory2.Activities.MainActivity.onCreate(MainActivity.java:51) 
        at android.app.Activity.performCreate(Activity.java:6439) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:179) 
        at android.app.ActivityThread.main(ActivityThread.java:5730) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 
     Caused by: android.content.res.Resources$NotFoundException: Resource "com.vestige.directory2:drawable/ic_plus_white" (7f07006b) is not a Drawable (color or path): TypedValue{t=0x1/d=0x7f07006b a=-1 r=0x7f07006b}
        at android.content.res.Resources.loadDrawableForCookie(Resources.java:2798)
        at android.content.res.Resources.loadDrawable(Resources.java:2732)
        at android.content.res.TypedArray.getDrawable(TypedArray.java:870)
        at android.widget.ImageView.<init>(ImageView.java:152)
        at android.widget.ImageButton.<init>(ImageButton.java:84)
        at android.widget.ImageButton.<init>(ImageButton.java:80)
        at com.google.android.material.internal.VisibilityAwareImageButton.<init>(VisibilityAwareImageButton.java:45)
        at com.google.android.material.floatingactionbutton.FloatingActionButton.<init>(FloatingActionButton.java:192)
        at com.google.android.material.floatingactionbutton.FloatingActionButton.<init>(FloatingActionButton.java:188)
        at java.lang.reflect.Constructor.newInstance(Native Method) 
        at android.view.LayoutInflater.createView(LayoutInflater.java:619) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:764) 
        at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704) 
        at android.view.LayoutInflater.rInflate(LayoutInflater.java:835) 
        at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:798) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:515) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:423) 
        at android.view.LayoutInflater.inflate(LayoutInflater.java:374) 
        at androidx.appcompat.app.AppCompatDelegateImpl.setContentView(AppCompatDelegateImpl.java:469) 
        at androidx.appcompat.app.AppCompatActivity.setContentView(AppCompatActivity.java:140) 
        at com.vestige.directory2.Activities.MainActivity.onCreate(MainActivity.java:51) 
        at android.app.Activity.performCreate(Activity.java:6439) 
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1108) 
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2498) 
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2605) 
        at android.app.ActivityThread.access$1100(ActivityThread.java:165) 
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1473) 
        at android.os.Handler.dispatchMessage(Handler.java:102) 
        at android.os.Looper.loop(Looper.java:179) 
        at android.app.ActivityThread.main(ActivityThread.java:5730) 
        at java.lang.reflect.Method.invoke(Native Method) 
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:791) 
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:681) 

我已经提到了许多解决方案,并且大多数人都在谈论将android:backgroundTint更改为app:backgroundTint,正如您所看到的,我什至没有使用。

我的 <com.google.android.material.floatingactionbutton.FloatingActionButton android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin" android:src="@drawable/ic_plus_white" /> 依赖项-

 protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);

        FloatingActionButton fab = findViewById(R.id.fab);

        fab.setOnClickListener(v -> addInformation());

我的styles.xml-

build.gradle(Module: app)

ic_plus_white.xml-

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'androidx.appcompat:appcompat:1.0.2'
    implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
    implementation 'com.google.android.material:material:1.0.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'androidx.test:runner:1.2.0'
    androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
    // Room components
    implementation "androidx.room:room-runtime:$rootProject.roomVersion"
    annotationProcessor "androidx.room:room-compiler:$rootProject.roomVersion"
    androidTestImplementation "androidx.room:room-testing:$rootProject.roomVersion"

    // Lifecycle components
    implementation "androidx.lifecycle:lifecycle-extensions:$rootProject.archLifecycleVersion"
    annotationProcessor "androidx.lifecycle:lifecycle-compiler:$rootProject.archLifecycleVersion"

    //Gson
    implementation 'com.google.code.gson:gson:2.8.5'
}

模拟器28上的API版本

Mobile 23上的API版本

已解决:只需将我的可绘制ic_plus_white.xml从drawable-v-24文件夹移动到drawable文件夹。解决了这个问题!

2 个答案:

答案 0 :(得分:0)

请确保您的应用或活动主题继承以下主题之一:

  • Theme.MaterialComponents
  • Theme.MaterialComponents.NoActionBar
  • Theme.MaterialComponents.Light
  • Theme.MaterialComponents.Light.NoActionBar
  • Theme.MaterialComponents.Light.DarkActionBar
  • Theme.MaterialComponents.DayNight
  • Theme.MaterialComponents.DayNight.NoActionBar
  • Theme.MaterialComponents.DayNight.DarkActionBar

您的drawable/ic_plus_white似乎不是有效的可绘制对象。也请确认其内容。

答案 1 :(得分:0)

您遇到的问题在启动活动XML的第26行上。如果您可以向我们展示该文件,我们也许可以提供帮助。