How to make FAB work in Fragment from a Activity and not in AppCompatActivity

淺唱寂寞╮ 提交于 2020-01-07 05:23:09

问题


I'm putting my FAB in a Fragment from an Activity and I keep getting this error:

10-01 21:01:55.238 22031-22031/com.vei.scanandgo E/AndroidRuntime: 
FATAL EXCEPTION: main
 Process: com.vei.scanandgo, PID: 22031
 android.view.InflateException: Binary XML file line #408: Error inflating class android.support.design.widget.FloatingActionButton
     at android.view.LayoutInflater.createView(LayoutInflater.java:620)
     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:696)
     at android.view.LayoutInflater.rInflate(LayoutInflater.java:755)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:492)
     at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
     at com.vei.scanandgo.BaseFragment.storeView(BaseFragment.java:317)
. . .
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
     at dalvik.system.NativeStart.main(Native Method)
  Caused by: java.lang.reflect.InvocationTargetException
     at java.lang.reflect.Constructor.constructNative(Native Method)
     at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
. . .
. ...
     at java.lang.reflect.Method.invoke(Method.java:515) 
     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786) 
     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602) 
     at dalvik.system.NativeStart.main(Native Method) 
  Caused by: java.lang.IllegalArgumentException: You need to use a Theme.AppCompat theme (or descendant) with the design library.
     at android.support.design.widget.ThemeUtils.checkAppCompatTheme(ThemeUtils.java:33)
     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:159)
     at android.support.design.widget.FloatingActionButton.<init>(FloatingActionButton.java:153)
. . 
. ..

Gradle is set to

compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:25.3.1'

Inflate is done by:

View mView = inflater.inflate(getlayoutResId(), container, false);

The only thing that I haven't changed is the Activity to AppCompatActivity which will hit a lot of existing codes. Do we have a way to make this FAB work with Activity?

FAB:

<android.support.design.widget.FloatingActionButton
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="16dp"
    android:src="@drawable/pichere"
    app:backgroundTint="@color/white"
    app:borderWidth="0dp" />

Thanks!

If I am making a wrong design, please advise on what else I can do to make this fragment with FAB work.


回答1:


Do we have a way to make this FAB work with Activity?

Not directly. android.support.design.widget.FloatingActionButton requires AppCompatActivity and Theme.AppCompat.

I have a cross-port of that widget in this library that works with a regular Activity (or FragmentActivity), but it is for a minSdkVersion of 21 or higher, as it requires Theme.Material.

There are other FAB implementations, such as this one, that do not require appcompat-v7 and do not require Theme.Material.



来源:https://stackoverflow.com/questions/46512732/how-to-make-fab-work-in-fragment-from-a-activity-and-not-in-appcompatactivity

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!