在Activity中使用FloatingActionButton会导致片段抛出InflateException

时间:2017-10-19 00:54:34

标签: android android-layout android-fragments

我在遇到问题时使用了FloatingActionButton,当我设置backgroundTint时会导致Activity抛出InflateException,而不设置backgroundTint就不会有这个异常,这是为什么?我尝试了多种变体,但它们似乎都没有效果。有任何想法吗? ,以下是我的代码

的xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/coordinator"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true">


    <android.support.v4.widget.SwipeRefreshLayout
        android:id="@+id/books_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="58dp">

        <android.support.v7.widget.RecyclerView
            android:id="@+id/list_of_books_rv"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </android.support.v4.widget.SwipeRefreshLayout>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/move_top"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/ten_dp"
        android:backgroundTint="#ffffff"
        android:src="@mipmap/ic_launcher"
        app:elevation="@dimen/ten_dp"
        app:fabSize="normal"
        app:layout_anchor="@id/list_of_books_rv"
        app:layout_anchorGravity="bottom|right"
        app:pressedTranslationZ="10.0dip"
        tools:targetApi="lollipop" />
</android.support.design.widget.CoordinatorLayout>

例外:

java.lang.RuntimeException: Unable to resume activity {library.houhan.com.hhlibrary/library.houhan.com.hhlibrary.MainActivity}: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class android.support.design.widget.FloatingActionButton
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3480)
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3518)
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790)
     at android.app.ActivityThread.-wrap11(ActivityThread.java)
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599)
     at android.os.Handler.dispatchMessage(Handler.java:111)
     at android.os.Looper.loop(Looper.java:207)
     at android.app.ActivityThread.main(ActivityThread.java:5985)
     at java.lang.reflect.Method.invoke(Native Method)
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939)
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800)
  Caused by: android.view.InflateException: Binary XML file line #23: Binary XML file line #23: Error inflating class android.support.design.widget.FloatingActionButton
     at android.view.LayoutInflater.inflate(LayoutInflater.java:545)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:429)
     at library.houhan.com.base.BaseFragment.onCreateView(BaseFragment.kt:27)
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
     at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3181)
     at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214)
     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:463)
     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:452)
     at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171)
     at android.app.Activity.performResume(Activity.java:6465)
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3459)
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3518) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599) 
     at android.os.Handler.dispatchMessage(Handler.java:111) 
     at android.os.Looper.loop(Looper.java:207) 
     at android.app.ActivityThread.main(ActivityThread.java:5985) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800) 
  Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class android.support.design.widget.FloatingActionButton
     at android.view.LayoutInflater.createView(LayoutInflater.java:651)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:844)
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:804)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:521)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
     at library.houhan.com.base.BaseFragment.onCreateView(BaseFragment.kt:27) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) 
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) 
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) 
     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) 
     at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221) 
     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3181) 
     at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214) 
     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:463) 
     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:452) 
     at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171) 
     at android.app.Activity.performResume(Activity.java:6465) 
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3459) 
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3518) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599) 
     at android.os.Handler.dispatchMessage(Handler.java:111) 
     at android.os.Looper.loop(Looper.java:207) 
     at android.app.ActivityThread.main(ActivityThread.java:5985) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800) 
  Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.newInstance(Native Method)
     at android.view.LayoutInflater.createView(LayoutInflater.java:625)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:770) 
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:710) 
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:844) 
     at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:804) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:521) 
     at android.view.LayoutInflater.inflate(LayoutInflater.java:429) 
     at library.houhan.com.base.BaseFragment.onCreateView(BaseFragment.kt:27) 
     at android.support.v4.app.Fragment.performCreateView(Fragment.java:2337) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419) 
     at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) 
     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) 
     at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799) 
     at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580) 
     at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367) 
     at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322) 
     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229) 
     at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221) 
     at android.support.v4.app.FragmentManagerImpl.dispatchResume(FragmentManager.java:3181) 
     at android.support.v4.app.FragmentController.dispatchResume(FragmentController.java:214) 
     at android.support.v4.app.FragmentActivity.onResumeFragments(FragmentActivity.java:463) 
     at android.support.v4.app.FragmentActivity.onPostResume(FragmentActivity.java:452) 
     at android.support.v7.app.AppCompatActivity.onPostResume(AppCompatActivity.java:171) 
     at android.app.Activity.performResume(Activity.java:6465) 
     at android.app.ActivityThread.performResumeActivity(ActivityThread.java:3459) 
     at android.app.ActivityThread.handleResumeActivity(ActivityThread.java:3518) 
     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2790) 
     at android.app.ActivityThread.-wrap11(ActivityThread.java) 
     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1599) 
     at android.os.Handler.dispatchMessage(Handler.java:111) 
     at android.os.Looper.loop(Looper.java:207) 
     at android.app.ActivityThread.main(ActivityThread.java:5985) 
     at java.lang.reflect.Method.invoke(Native Method) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:939) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:800) 
  Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.graphics.drawable.Drawable android.graphics.drawable.Drawable$ConstantState.newDrawable()' on a null object reference
     at android.graphics.drawable.LayerDrawable$ChildDrawable.<init>(LayerDrawable.java:1723)
    at android.graphics.drawable.LayerDrawabl

1 个答案:

答案 0 :(得分:0)

我将android: backgourdTint修改为app: backgourdTint,然后不再抛出此异常