1

I have created the navigation drawer menu and now i want it to appear in my main activity.

I have the following code in my main activity:

    public class MainActivity extends sideMenu {

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

        FrameLayout contentFrameLayout = (FrameLayout) findViewById(R.id.content_frame);
        getLayoutInflater().inflate(R.layout.activity_main, contentFrameLayout);

    }}

When i run the app it crashes when entering main activity and it tells this:

**Caused by: java.lang.IllegalStateException: This Activity already has an action bar supplied by the window decor. Do not request Window.FEATURE_SUPPORT_ACTION_BAR and set windowActionBar to false in your theme to use a Toolbar instead.**

It specifies the error is in two places:
Menu activity -

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

Main activity -

    super.onCreate(savedInstanceState);


If someone can shed some light here i'd be grateful!

EDIT:

Android manifest:

<?xml version="1.0" encoding="utf-8"?>

<!-- To auto-complete the email text field in the login form with the user's emails -->
<uses-permission android:name="android.permission.GET_ACCOUNTS" />
<uses-permission android:name="android.permission.READ_PROFILE" />
<uses-permission android:name="android.permission.READ_CONTACTS" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity
        android:name=".LoginActivity"
        android:label="@string/app_name">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <activity
        android:name=".MainActivity"
        android:theme="@style/AppTheme.NoActionBar">
    </activity>
    <activity android:name=".Registo" />
    <activity android:name=".ListaEmpresas" />
    <activity android:name=".ListaFavoritos" />
    <activity android:name=".Candidaturas" />
    <activity
        android:name=".sideMenu"
        android:label="@string/title_activity_side_menu"
        android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

menu activity code:

public class sideMenu extends AppCompatActivity
        implements NavigationView.OnNavigationItemSelectedListener {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_side_menu);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); 
setSupportActionBar(toolbar);
        setSupportActionBar(toolbar);

        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawer, toolbar, R.string.navigation_drawer_open, 
R.string.navigation_drawer_close);
        drawer.addDrawerListener(toggle);
        toggle.syncState();

        NavigationView navigationView = (NavigationView) 
findViewById(R.id.navigation_view);
        navigationView.setNavigationItemSelectedListener(this);
    }

    @Override
    public void onBackPressed() {
        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        if (drawer.isDrawerOpen(GravityCompat.START)) {
            drawer.closeDrawer(GravityCompat.START);
        } else {
            super.onBackPressed();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is 
present.
        getMenuInflater().inflate(R.menu.side_menu, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }

    @SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(MenuItem item) {
        // Handle navigation view item clicks here.
        int id = item.getItemId();

        if (id == R.id.nav_camera) {
            // Handle the camera action
        } else if (id == R.id.nav_gallery) {

        } else if (id == R.id.nav_slideshow) {

        } else if (id == R.id.nav_manage) {

        } else if (id == R.id.nav_share) {

        } else if (id == R.id.nav_send) {

        }

        DrawerLayout drawer = (DrawerLayout) 
findViewById(R.id.drawer_layout);
        drawer.closeDrawer(GravityCompat.START);
        return true;
    }
}

Menu XML:

<?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"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/content_frame"/>

</LinearLayout>

<include
    layout="@layout/app_bar_side_menu"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

<android.support.design.widget.NavigationView
    android:id="@+id/navigation_view"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    app:headerLayout="@layout/nav_header_side_menu"
    app:menu="@menu/activity_side_menu_drawer" />

app_bar_side_menu xml:

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

<android.support.design.widget.AppBarLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:theme="@style/AppTheme.AppBarOverlay">

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

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

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

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

Content_side_menu xml:

    <?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="amsi.dei.estg.ipleiria.pt.ima.sideMenu"
tools:showIn="@layout/app_bar_side_menu">

</android.support.constraint.ConstraintLayout>

nav_header_side_menu:

<?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:layout_width="match_parent"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:backgroundTint="@android:color/holo_red_dark"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    app:srcCompat="@mipmap/ic_launcher_round" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="IMA"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="NOME USER" />
</LinearLayout>
Diogo Carvalho
  • 255
  • 1
  • 7
  • 15
  • Possible duplicate of [Do not request Window.FEATURE\_ACTION\_BAR issue](https://stackoverflow.com/questions/30923403/do-not-request-window-feature-action-bar-issue) – patrick.elmquist Nov 24 '17 at 13:25

2 Answers2

1

Your theme already has an ActionBar so you will have to convert it to NoActionBar.

For Whole App:
Open the styles.xml and make sure the last line of the parent has a word NoActionBar for example :

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

For An Activity:
Or you can just change it for one particular activity in the Manifest like by adding this atrribute in the Manifest :

android:theme="@style/Theme.AppCompat.Light.NoActionBar"
Xenolion
  • 12,035
  • 7
  • 33
  • 48
  • I changed it in the manifest. It doesn't crash anymore but still doesn't show the menu in my main activity, as I now have no action bar, which is the place it should appear i guess. – Diogo Carvalho Nov 24 '17 at 13:30
  • Call this line just after setContentView: make sure you call this just after setContentView `Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar);` – Xenolion Nov 24 '17 at 13:35
  • Check my last comment and try it out @DiogoCarvalho – Xenolion Nov 24 '17 at 13:38
  • Still not showing. I have the toolbar in the app_bar xml styled like this: `` – Diogo Carvalho Nov 24 '17 at 13:40
  • Give me a favour go to your question and click edit then edit by adding all xml and java code that may be useful I need to pass to check it all and I will figure why the error exists currently I think there are parts of your code I dont see?? – Xenolion Nov 24 '17 at 13:43
  • Did you have a chance to look at it? – Diogo Carvalho Nov 24 '17 at 16:39
  • 1
    Forgive me @DiogoCarvalho I got offline. There is a logical error in your code; in your main activity side menu is a class that extends appcompatActvity so because the layout has been set there you do not have to do it twice call it again here `setContentView(R.layout.activity_main);` when you call `super.onCreate(savedInstanceState);` is enough because the superclass is now `sidemenu` and so dont setcontent again. and this may result to you having a null pointer for your framelayout. – Xenolion Nov 24 '17 at 23:05
  • Your codding issues are no longer accepted in stackoverflow. You better check me directly I can surely and freely help you with that! `johnnnoni@gmail.com`. Lets solve the problem afresh and recommended way of how you want the app to be!! – Xenolion Nov 24 '17 at 23:09
  • Thanks man, i'll give it a try as soon as i get home and i'll contact you if it doesn't get solved – Diogo Carvalho Nov 25 '17 at 10:56
  • Okay see worry out! – Xenolion Nov 25 '17 at 10:57
  • Okay I am glad you solved the problem **Happy Coding!** pal. – Xenolion Nov 25 '17 at 12:56
0

use this theme.

<style name="AppTheme.NoActionBar">
    <item name="windowActionBar">false</item>
    <item name="windowNoTitle">true</item>
</style>
mehul chauhan
  • 1,792
  • 11
  • 26