0

I'm trying to add a bottom sheet to my layout, so I place all my fragment's layout inside a CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools">

    <RelativeLayout
        android:id="@+id/myOriginalLayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:animateLayoutChanges="true"
        android:clickable="true">

       ....
       ....

    </RelativeLayout>

    <FrameLayout
        android:id="@+id/bottom_sheet"
        android:layout_width="match_parent"
        android:layout_height="150dp"
        app:layout_behavior="android.support.design.widget.BottomSheetBehavior">
    </FrameLayout>

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

It keeps on crashing though when I try to inflate the layout, stating Error inflating class android.support.design.widget.CoordinatorLayout.

BVtp
  • 2,308
  • 2
  • 29
  • 68
  • 1
    Have you added `AppCompat` ? – Jay Rathod May 24 '16 at 10:58
  • yes.. `compile "com.android.support:appcompat-v7:23.4.+"` but it doesn't help – BVtp May 24 '16 at 11:12
  • Post your `Gradle` above. – Jay Rathod May 24 '16 at 11:18
  • ok.. I've added it – BVtp May 24 '16 at 11:24
  • Give a try by changing this `buildToolsVersion '24.0.0-rc3'` add dash. – Jay Rathod May 24 '16 at 11:35
  • still no luck... could it be perhaps due to the fact that my activity extends Activity instead of AppCompatActivity ? – BVtp May 24 '16 at 11:44
  • Is it showing error Project Run Time or while creating Layout ? – Jay Rathod May 24 '16 at 11:45
  • it just crashes when inflating the layout as depicted in the post (`Error inflating class android.support.design.widget.CoordinatorLayout`) – BVtp May 24 '16 at 11:47
  • You mean while designing your `Layout` at that time it is throwing you in design mode `(Error inflating class android.support.design.widget.CoordinatorLayout)` Right. – Jay Rathod May 24 '16 at 11:48
  • nope.. I added a screenshot of what the layout designer in android studio shows me. The app is able to run though, it only crashes when I get to that fragment (more specifically - in onCreateView when trying to inflate the layout) – BVtp May 24 '16 at 11:54
  • Have you tried from `Activity` to `AppCompatActivity` ? If not then Try and Check. – Jay Rathod May 24 '16 at 11:55
  • I have tried , but it creates another crash if I do that (change Activity to AppCompatActivity) : `java.lang.IllegalStateException: You need to use a Theme.AppCompat theme (or descendant) with this activity.` – BVtp May 24 '16 at 11:59
  • Try with my answer may be will solve that. – Jay Rathod May 24 '16 at 12:02

2 Answers2

0

Use this theme by Changing from Activity with AppCompatActivity.

    <style name="Theme.DesignDemo" parent="Base.Theme.DesignDemo">
    </style>

    <style name="Base.Theme.DesignDemo" parent="Theme.AppCompat.Light.NoActionBar">
        <item name="colorPrimary">#673AB7</item>
        <item name="colorPrimaryDark">#512DA8</item>
        <item name="colorAccent">#FF4081</item>
        <item name="android:windowBackground">@color/window_background</item>
    </style>

Change your buildToolsVersion to 23.0.3. Then Sync your Gradle Again.

android {
    compileSdkVersion 23
    buildToolsVersion "23.0.3"

    defaultConfig {
        applicationId ""
        minSdkVersion 15
        targetSdkVersion 23
        versionCode 1
        versionName "1.0"
    }
}

And add this Dependencies.

dependencies {
    compile 'com.android.support:appcompat-v7:23.4.0'
    compile 'com.android.support:design:23.4.0'
    compile 'com.android.support:cardview-v7:23.4.0'
}
Jay Rathod
  • 11,131
  • 6
  • 34
  • 58
  • crashes unfortunately – BVtp May 24 '16 at 12:21
  • Can you tell me where and which crashed ? This settings working for me fine enough. – Jay Rathod May 24 '16 at 12:24
  • yes, of course, the exception is : `java.lang.NullPointerException: Attempt to invoke virtual method 'void android.app.ActionBar.setDisplayOptions(int)' on a null object reference`. the line it crashes at is : `actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM); ` – BVtp May 24 '16 at 12:25
  • I have a class of ActionBar handler so it causes problems – BVtp May 24 '16 at 12:26
  • For which purpose you have created. You should use `ToolBar` only with `AppCompatActivity`. – Jay Rathod May 24 '16 at 12:27
  • @BVtp Make `AppCompatActivity` to `ActionBarActivity` and check. it will work. – Jay Rathod May 24 '16 at 12:29
  • Yes it will not :). Because only work toolbar with `AppCompatActivity`. Check my answer here http://stackoverflow.com/questions/37353160/action-bar-not-shown-in-android-studio/37353507#37353507 – Jay Rathod May 24 '16 at 12:36
  • so if I use a custom ActionBar I can't use AppCompatActivity? – BVtp May 24 '16 at 12:45
  • @BVtp Nope bro :). You must use `ToolBar` as per Material Design Concept. Appreciate my answer if it is useful to you. – Jay Rathod May 24 '16 at 12:49
  • So basically the use of Activity instead of AppCompatActivity prevents me from using CoordinatorLayout? – BVtp May 24 '16 at 12:50
  • @BVtp Yes as Per New Material design concepts needful to use what i have all suggested thanks :). – Jay Rathod May 24 '16 at 12:52
  • I'm a bit confused.. I need to erase the ActionBar I'm using entirely? and implement a whole new toolbar instead? Or did I misunderstand you? – BVtp May 24 '16 at 12:53
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/112793/discussion-between-jaydroider-and-bvtp). – Jay Rathod May 24 '16 at 12:54
  • If you want to use `Coordinator Layout` and all that then you must need to change. ToolBar` will set dynamic Titles for you for each `Activity`. – Jay Rathod May 24 '16 at 12:56
  • Sounds like a lot of work, converting from actionbar to toolbar.. :/ – BVtp May 24 '16 at 12:57
  • Changes bit will do the trick for you just follow my answer above which `Link` is provided in comment. and also lots of tutorial are their for dynamic `Tool Bar`. – Jay Rathod May 24 '16 at 12:59
  • Thanks bro for appreciation up-vote it also so it will be helpful to others also thanks :). and let me know if you need any further help good luck :). – Jay Rathod May 24 '16 at 13:06
  • hmmm. This shouldn't be the answer. It didn't solve the issue. – Shumin May 24 '16 at 17:30
  • https://guides.codepath.com/android/Extended-ActionBar-Guide - here AppCompatActivity is used with ActionBar. How does that work there and in my case it doesn't? – BVtp May 25 '16 at 14:33
  • I'm sorry, but you were wrong.. I was able to leave my ActionBar and use AppCompatActivity. All I had to do is change my AcrionBar to support.v7.ActionBar. – BVtp May 26 '16 at 08:36
0

The solution that solved the issue :

  1. The activity I was using was extending Activity, instead of AppCompatActivity, so that had to be changed.
  2. However, when changing Activity to AppCompatActivity I still had crashes because I was using the regular ActionBar. So the steps I had taken to fix that:

    a. call `getSupportActionBar()` instead of `getActionBar()`
    b. change `ActionBar` to `support.v7.app.ActionBar`
    
BVtp
  • 2,308
  • 2
  • 29
  • 68