-2

I'm trying to add a bottom sheet in one of my apps. I know how to use them and I implemented one while following a medium tutorial (and reading the problems people had in Stack Overflow) which works just fine.

But when I try to replicate that in my project, it keeps crashing no matter if I put it in the main Activity or inside a Fragment. It's driving me crazy as it crashes even if I copy the same xmls and procedure from the example I have working.

activity_main.xml

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

<!-- Toolbar -->
<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppBarOverlay">

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="?android:attr/actionBarSize"
        android:background="@color/colorPrimary"
        app:popupTheme="@style/PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<!-- Fragment -->
<FrameLayout
    android:id="@+id/main_screen_container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_marginTop="?android:attr/actionBarSize"/>

<!-- Bottom navigation view -->
<android.support.design.widget.BottomNavigationView
    android:id="@+id/bottom_navigation"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    app:itemBackground="@color/colorPrimary"
    app:itemIconTint="@color/colorGreyLight"
    app:itemTextColor="@color/colorGreyLight"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_menu" />

That frameLayout is populated with the multiple fragments of my App like this:

private fun showNewFragment(fragmentToShow: Fragment, addToBackStack: Boolean = true) {
    try {
        if (activity.supportFragmentManager != null) {
            activity.supportFragmentManager.popBackStack(null,
                    FragmentManager.POP_BACK_STACK_INCLUSIVE)

            val fragmentManager = activity.supportFragmentManager
            val transaction = fragmentManager.beginTransaction()
            transaction.setTransition(FragmentTransaction.TRANSIT_NONE)
            transaction.replace(fragmentPlaceholder, fragmentToShow)

            if (addToBackStack) {
                transaction.addToBackStack(null)
            }
            transaction.commit()
            fragmentManager.executePendingTransactions()
        }
    } catch (illegalState: IllegalStateException) {
        illegalState.printStackTrace()
    }
}

This is the layout of the fragment I'm trying to place in the framelayout, fragment_lesson_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.trabajofindegrado.ui.screens.lessonview.LessonViewFragment">

<TextView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:text="@string/hello_blank_fragment" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_margin="@dimen/fab_margin"
    android:src="@drawable/ic_message"
    app:layout_anchor="@+id/bottom_sheet_filters"
    app:layout_anchorGravity="top|end" />

<include
    layout="@layout/bottom_sheet_filters"
    />

I think the problem may be here as the FloatingActionButton is not anchoring the layout properly, like it can't find it.

The fragment, just empty to test it:

class LessonViewFragment : Fragment() {
    companion object {
        val TAG = LessonViewFragment::class.java.simpleName

        val startIntent: Intent
            get() = Intent(TAG)
    }

    override fun onCreateView(inflater: LayoutInflater, 
                          container: ViewGroup?,
                          savedInstanceState: Bundle?): View? =
        inflater.inflate(R.layout.fragment_lesson_view, container, false)

}

And finally, here is bottom_sheet_filters.xml, which I don't think it has a problem as it's 100% copied from the example I did:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="340dp"
android:background="@android:color/darker_gray"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="@dimen/peek_height"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior">

<TextView
    android:id="@+id/navigation"
    android:layout_width="match_parent"
    android:layout_height="@dimen/peek_height"
    android:background="?android:attr/windowBackground"
    android:text="@string/sheet_title" />

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <TextView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:gravity="center"
        android:text="@string/very_large_test"
        android:textColor="@android:color/white" />

</android.support.v4.widget.NestedScrollView>

</LinearLayout>

It doesn't seem to be a problem with it being inside of a fragment either, I tried using a fragment in the example and it works, and then I tried to move the BottomSheet to the activity and it keeps crashing. The log:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.trabajofindegrado/com.trabajofindegrado.activities.application.ApplicationActivity}: android.view.InflateException: Binary XML file line #32: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: android.view.InflateException: Binary XML file line #32: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
                   Caused by: java.lang.RuntimeException: Could not inflate Behavior subclass android.support.design.widget.BottomSheetBehavior
                      at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:628)
                      at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795)
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703)
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112)
                      at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989)
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:854)
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426)
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:377)
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287)
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139)
                      at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50)
                      at android.app.Activity.performCreate(Activity.java:6720)
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618)
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
                      at android.app.ActivityThread.-wrap12(ActivityThread.java)
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
                      at android.os.Handler.dispatchMessage(Handler.java:102)
                      at android.os.Looper.loop(Looper.java:154)
                      at android.app.ActivityThread.main(ActivityThread.java:6119)
                      at java.lang.reflect.Method.invoke(Native Method)
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
                   Caused by: java.lang.reflect.InvocationTargetException
                      at java.lang.reflect.Constructor.newInstance0(Native Method)
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:430)
                      at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:626)
                      at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795) 
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703) 
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112) 
                      at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:854) 
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                      at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50) 
                      at android.app.Activity.performCreate(Activity.java:6720) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 
                   Caused by: java.lang.UnsupportedOperationException: Can't convert value at index 1 to dimension: type=0x4
                      at android.content.res.TypedArray.getDimensionPixelSize(TypedArray.java:720)
                      at android.support.design.widget.BottomSheetBehavior.<init>(BottomSheetBehavior.java:187)
                      at java.lang.reflect.Constructor.newInstance0(Native Method) 
                      at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
                      at android.support.design.widget.CoordinatorLayout.parseBehavior(CoordinatorLayout.java:626) 
                      at android.support.design.widget.CoordinatorLayout$LayoutParams.<init>(CoordinatorLayout.java:2795) 
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:1703) 
                      at android.support.design.widget.CoordinatorLayout.generateLayoutParams(CoordinatorLayout.java:112) 
                      at android.view.LayoutInflater.parseInclude(LayoutInflater.java:989) 
                      at android.view.LayoutInflater.rInflate(LayoutInflater.java:854) 
                      at android.view.LayoutInflater.rInflateChildren(LayoutInflater.java:821) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:518) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:426) 
                      at android.view.LayoutInflater.inflate(LayoutInflater.java:377) 
                      at android.support.v7.app.AppCompatDelegateImplV9.setContentView(AppCompatDelegateImplV9.java:287) 
                      at android.support.v7.app.AppCompatActivity.setContentView(AppCompatActivity.java:139) 
                      at com.trabajofindegrado.activities.application.ApplicationActivity.onCreate(ApplicationActivity.kt:50) 
                      at android.app.Activity.performCreate(Activity.java:6720) 
                      at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                      at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2618) 
                      at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726) 
                      at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                      at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477) 
                      at android.os.Handler.dispatchMessage(Handler.java:102) 
                      at android.os.Looper.loop(Looper.java:154) 
                      at android.app.ActivityThread.main(ActivityThread.java:6119) 
                      at java.lang.reflect.Method.invoke(Native Method) 
                      at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886) 
                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776) 

Thanks in advance

kirchhoff
  • 206
  • 4
  • 12
  • 1
    `Can't convert value at index 1 to dimension:` check your dimensions . – ADM May 12 '18 at 11:54
  • 1
    Best advice ever. Thanks. How stupid I am, a whole day changing stuff and the problem was I had "80" instead of "80dp" in the dimens. – kirchhoff May 12 '18 at 18:17

1 Answers1

1

Couldn't comment > insufficient reputation points.

My 2 cents: Not entirely sure, but shouldn't all design support library widgets be direct children of the CoordinatorLayout ?

Make sure all your other dimensions are correct (have correct units, consistent with other dimens e.g. peek_height not greater than the bottom sheet itself), too.

Also, you are nesting CoordinatorLayouts, I doubt you have the best performance numbers there. I suggest you find a better design.

I hope this answers your question

Ace
  • 2,108
  • 22
  • 29
  • I tried removing the ?android:attr reference and using my own dimen for that: No difference. About the _"shouldn't the CoordinatorLayout encapsulate all these design library widgets you are using"_ I don't know exactly what you mean but my CoordinatorLayout has the exact same stuff than the example I did – kirchhoff May 12 '18 at 13:26
  • I mainly use `ConstraintLayout` in every fragment, the amount of `LinearLayout` and `RelativeLayouts` I use is so low, I just added CoordinatorLayout to the `Activity` and this `Fragment` in order to use the BottomSheet, in total, I have at the most 3 levels of nesting, plus, the tag is just a way to keep the layouts more organized. I tried to copy the whole BottomSheet code inside the fragment but it keeps crashing (it did work in the example) – kirchhoff May 12 '18 at 15:15
  • first, try to make it work by writing everything in the fragment layout, later when it works, move it to another file. Also, while doing this, when you get the error, in the stack trace, usually the execptions are clickable, this really helps with pinpointing your mistakes. From the stack trace above, we can tell a couple of things: 1) your error is in the `xml` files. 2) you error is related to a dimension. 3) your error is directly related to a view with a `Behavior` – Ace May 12 '18 at 15:28
  • 1
    from the docs, `UnsupportedOperationException` is thrown when the attribute is defined, but not a dimension. try using hardcoded dimensions first, see if that works – Ace May 12 '18 at 15:34
  • I had an error, my peek_height didn't have the "dp" in the end. Nice answer overall. It was your idea of hardcoding the fields manually that made me realize my mistake. Thanks – kirchhoff May 12 '18 at 18:18
  • Let us [continue this discussion in chat](https://chat.stackoverflow.com/rooms/170943/discussion-between-ace-and-kirchhoff). – Ace May 13 '18 at 08:19