0

I am trying to setup navigation drawer "without" AppCompat. I am getting an error You need to use a Theme.AppCompat theme (or descendant) with the design library Any ideas?

I setup my `styles.xml as:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="@android:style/Theme.Material.Light">
        <!-- Customize your theme here. -->
    </style>
</resources>

and in my AndroidManifest.xml file I have the android:theme="@style/AppTheme" only.

My activity_main.xml has:

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

    <RelativeLayout 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">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Hello World!" />

    </RelativeLayout>
    <!-- The navigation drawer -->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_main"
        app:menu="@menu/activity_main_drawer" />
</android.support.v4.widget.DrawerLayout>

nav_header_main and activity_main drawer contents are same as from android studio examples and my MainActivity onCreate function is pretty straightforward:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
}

My build.bradle dependencies are:

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])
    implementation 'com.android.support:support-v13:26.1.0'
    implementation 'com.android.support:design:26.1.0'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.1'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.1'
}

The error is:

12-08 15:11:19.095 16936-16936/? E/AndroidRuntime: FATAL EXCEPTION: main
                                                   Process: com.xxx.xxx, PID: 16936
                                                   java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxx.xxx/com.xxx.xxx.MainActivity}: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2684)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                    Caused by: android.view.InflateException: Binary XML file line #0: Error inflating class android.support.design.widget.NavigationView
                                                    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.view.LayoutInflater.createView(LayoutInflater.java:645)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787)
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727)
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:858)
                                                       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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412)
                                                       at android.app.Activity.setContentView(Activity.java:2415)
                                                       at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:24)
                                                       at android.app.Activity.performCreate(Activity.java:6684)
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119)
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637)
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751)
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java)
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496)
                                                       at android.os.Handler.dispatchMessage(Handler.java:102)
                                                       at android.os.Looper.loop(Looper.java:154)
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186)
                                                       at java.lang.reflect.Method.invoke(Native Method)
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889)
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779)
                                                    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.NavigationView.<init>(NavigationView.java:104)
                                                       at android.support.design.widget.NavigationView.<init>(NavigationView.java:98)
                                                       at java.lang.reflect.Constructor.newInstance0(Native Method) 
                                                       at java.lang.reflect.Constructor.newInstance(Constructor.java:430) 
                                                       at android.view.LayoutInflater.createView(LayoutInflater.java:645) 
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:787) 
                                                       at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:727) 
                                                       at android.view.LayoutInflater.rInflate(LayoutInflater.java:858) 
                                                       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 com.android.internal.policy.PhoneWindow.setContentView(PhoneWindow.java:412) 
                                                       at android.app.Activity.setContentView(Activity.java:2415) 
                                                       at com.xxx.xxx.MainActivity.onCreate(MainActivity.java:24) 
                                                       at android.app.Activity.performCreate(Activity.java:6684) 
                                                       at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1119) 
                                                       at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2637) 
                                                       at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2751) 
                                                       at android.app.ActivityThread.-wrap12(ActivityThread.java) 
                                                       at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1496) 
                                                       at android.os.Handler.dispatchMessage(Handler.java:102) 
                                                       at android.os.Looper.loop(Looper.java:154) 
                                                       at android.app.ActivityThread.main(ActivityThread.java:6186) 
                                                       at java.lang.reflect.Method.invoke(Native Method) 
                                                       at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:889) 
                                                       at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:779) 
Evren Yurtesen
  • 2,267
  • 1
  • 22
  • 40

2 Answers2

2
implementation 'com.android.support:design:26.1.0'

Anything you pull in from this artifact will require appcompat-v7, Theme.AppCompat, etc. In your case, NavigationView is from this library.

I do not have NavigationView in my cross-port of the Design widgets to Theme.Material. AFAIK, you will need to drop this widget and use something else for the drawer contents (e.g., RecyclerView).

Note that DrawerLayout does not require appcompat-v7, so that part is fine.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I think you are right. If I switch to `parent="Theme.AppCompat.Light.DarkActionBar"` in styles.xml then app function but the `ActionBar` is missing. How can I get the `ActionBar` in an activity "easily" ? – Evren Yurtesen Dec 08 '17 at 14:07
  • @EvrenYurtesen: Your question is about how *not* to use that theme. If you want to use something based on `Theme.AppCompat`, you need to inherit from `AppCompatActivity`, among other things. – CommonsWare Dec 08 '17 at 14:14
0
<style name="AppTheme.Base" parent="Theme.AppCompat.Light.NoActionBar">

    <item name="colorPrimary">#873AB0</item>
    <item name="colorPrimaryDark">#256DA8</item>
    <item name="colorAccent">#11BCD4</item>
    <item name="android:textColorPrimary">#212222</item>
    <item name="android:textColorSecondary">#646464</item>

</style>

from above snippet remove

        <item name="android:textColorPrimary">#212222</item>
        <item name="android:textColorSecondary">#646464</item>

check Example

Hope this helps you.

InsaneCat
  • 2,115
  • 5
  • 21
  • 40
  • Are you copy/paste'ing others answers? https://stackoverflow.com/questions/30709419/error-inflating-class-android-support-design-widget-navigationview/34511285 In that question the OP is using AppCompat theme... – Evren Yurtesen Dec 08 '17 at 13:42
  • Nope it's releevnt to this answer. if it's helps you – InsaneCat Dec 08 '17 at 13:43
  • It is not relevant at all, because everything works fine if I switch to an AppCompat theme. For example if I just set `parent="Theme.AppCompat.Light.DarkActionBar"` in my styles.xml the drawer works fine. Totally irrelevant answers, you should try to understand the question. I am trying to figure out if it is possible to solve the issue without using AppCompat theme. Dont you think I already searched all answers to similar questions at stackoverflow? :) thats how I knew you copy/pasted – Evren Yurtesen Dec 08 '17 at 13:45
  • Did uyou seen i'm putting "Theme.AppCompat.Light.NoActionBar" there in style. if thsi answer is not gave any help then i'll delete this for sure. waiting for your response. – InsaneCat Dec 08 '17 at 13:46
  • The problem is that I want the default action bar and if I put an AppCompat theme it disappears. Even if I select `Theme.AppCompat.Light.DarkActionBar` – Evren Yurtesen Dec 08 '17 at 13:50
  • if you put "Theme.AppCompat.Light.DarkActionBar" then also got same problem? – InsaneCat Dec 08 '17 at 13:52
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/160800/discussion-between-evren-yurtesen-and-insanecat). – Evren Yurtesen Dec 08 '17 at 14:07