7

Hi I have to change my App theme to "Theme.AppCompat.Light.NoActionBar". But after I changed it, the app crashes. I could not find out where the problem is :/

My goal is to expand my Navigation Drawer over the toolbar. But for that, I need the "NoActionBar" function. I hope someone can help me out. Thanks forwards!

That is the error code from the debugger:

12-23 11:04:28.292 1410-1421/? E/ANDR-PERF-LOCK: Failed to apply optimization for resource: 4 level: 0

That is my styles.xml code:

<resources>
    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>

        <item name="android:listDivider">@android:color/transparent</item>
    </style>

    <style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
    </style>

    <style name="AppTheme.AppBarOverlay" parent="ThemeOverlay.AppCompat.Dark.ActionBar" />

    <style name="AppTheme.PopupOverlay" parent="ThemeOverlay.AppCompat.Light" />

</resources>
Zoe
  • 27,060
  • 21
  • 118
  • 148
Sinan Kara
  • 117
  • 1
  • 2
  • 11

3 Answers3

3

Focusing on your style

  <style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">

This theme Theme.AppCompat.Light.NoActionBar means you are telling android not to take default ActionBar

and than Later you are calling ActionBar in your MainAcitvity which is the cause for your crash.

Instead use Toolbar and set as ActionBar :

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);

setSupportActionBar(toolbar);

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

Also I think you are using lolipop so add this in your styles.xml :

<style name="AppTheme.NoActionBar">
        <item name="windowActionBar">false</item>
        <item name="windowNoTitle">true</item>
</style>

And in your manifest :

<activity android:name=".activity.YourActivity"
          android:theme="@style/AppTheme.NoActionBar"><!-- ADD THIS LINE -->
Abubakker Moallim
  • 1,610
  • 10
  • 20
3

1.Go to MainActivity.java file
2.Comment this line of code
//NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
3.crash should be fixed

Reason why You're using "Bottom Navigation Activity", the reason it crashing because java tries to get the object from Actionbar, When the Actionbar is disabled it returns null cause we don't have an action bar, To fix this you need to comment or remove code out if you go to MainActivity.java (in case you're using java) you will see this code on line 26 and it tries to get set up the action bar. we can comment the code out and it should work fine.

Sorry for my bad English.

NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);

CENSOR_1337
  • 31
  • 1
  • 3
  • It solves the crash problem but the Bottom Navigation doesn't work. Any solutions for making it work with the navigation without the `ActionBar`? – Joshua Jun 02 '21 at 21:11
  • 1
    NavigationUI.setupWithNavController(binding.bottomNavView, navController); – Andrea Oct 12 '21 at 12:21
0

The solution is easy, there are two ways to change your actionBar:

  1. You could change under src/main/AndroidManifest.xml:
android:theme="@style/Theme.AppCompat.DayNight.NoActionBar"

which will cause crash.

  1. You could also change it under src/main/res/values/themes.xml:

If you change it such as:

<style name="Theme.MyQuizApp"
parent="Theme.MaterialComponents.DayNight.NoActionBar">

It should solve the problem.

sweak
  • 1,369
  • 2
  • 6
  • 21