3

I programmed a quiz with 3 Activities: 1 = QuizActivity, 2= Menu2 and 3= Menu3. Activity 1 and 2 are working very fine. The problem is in Menu3. There is one code of line which is creating a Nullpoiner excption: void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)' on a null object reference when I delete this line I can start (by clicking the about item on the Navigation drawer(leads to Menu3 activity)) the menu3 activity. The only problem is when i leave out this line the "Burger icon" (you can see it on the picture) is not there anymore. When i leave the code of line I can`t navigate to Menu3 because when I do that the app force closes. How can i solve this problem?

this part is  not appearing

Menu3 java:

       package amapps.impossiblequiz;



import static amapps.impossiblequiz.R.id.nv3;

public class Menu3 extends AppCompatActivity {

private DrawerLayout mDrawerLayout3;
private ActionBarDrawerToggle mToggle;

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



    Toolbar mToolbar = (Toolbar) findViewById(R.id.nav_action);
    setSupportActionBar(mToolbar);
    mDrawerLayout3 = (DrawerLayout) findViewById(R.id.drawerLayout3);

    mToggle = new ActionBarDrawerToggle(this, mDrawerLayout3, R.string.open, R.string.close);
    mDrawerLayout3.addDrawerListener(mToggle);
    mToggle.syncState();
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);

    NavigationView mNavigationView = (NavigationView) findViewById(nv3);
    mNavigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {

        @Override
        public boolean onNavigationItemSelected(MenuItem menuItem){
            switch (menuItem.getItemId()){
                case(R.id.nav_home3):
                    Intent accountActivity3 = new Intent(getApplicationContext(),QuizActivity.class);
                    startActivity(accountActivity3);

            }
            return true;
        }
    });
}


@Override //Makes that the "Burger" Item, shows the Drawer if someone clicks on the simbol
public boolean onOptionsItemSelected(MenuItem item) {
    if (mToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);


}

}

Menu3 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="amapps.impossiblequiz.Menu3"
android:id="@+id/drawerLayout3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">


<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_alignParentEnd="true"
    android:layout_alignParentRight="true"
    android:layout_marginBottom="215dp"
    android:layout_marginEnd="42dp"
    android:layout_marginRight="42dp"
    android:text="Button" />
</RelativeLayout>

<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu3"
    android:layout_gravity="start"
    android:id="@+id/nv3"
    app:headerLayout="@layout/navigation_header"
    app:itemIconTint="@drawable/tint_color_selector2">




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

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

Menu2 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:layout_width="match_parent"
         android:layout_height="match_parent"
         tools:context="amapps.impossiblequiz.Menu2"
         android:id="@+id/drawerLayout2"
         android:orientation="vertical">


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


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


    <RelativeLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent">




        <TextView
            android:id="@+id/textScore2"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#f60"
            android:paddingTop="10dp"
            android:paddingLeft="35dp"
            android:text="Your score is:"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_marginTop="28dp"
            android:layout_below="@+id/textView2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <TextView
            android:id="@+id/textHighScore"
            android:layout_width="wrap_content"
            android:layout_height="50dp"
            android:background="#FFAB00"
            android:paddingLeft="35dp"
            android:paddingRight="100dp"
            android:paddingTop="10dp"
            android:text="Highest score:"
            android:textAppearance="@style/TextAppearance.AppCompat"
            android:textColor="#ffffff"
            android:textSize="20dp"
            android:layout_marginTop="13dp"
            android:layout_below="@+id/textScore2"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <Button
            android:id="@+id/tryAgain_button"
            android:layout_width="150dp"
            android:layout_height="50dp"
            android:background="@drawable/bg_pill"
            android:onClick="onClick"
            android:text="Restart Quiz!"
            android:textColor="#ffffff"
            android:layout_marginTop="18dp"
            android:layout_below="@+id/textHighScore"
            android:layout_centerHorizontal="true" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="33dp"
            android:text="Stats: play and get trophies!"
            android:textAppearance="@style/TextAppearance.AppCompat.Button"
            android:textSize="20dp"
            android:layout_alignParentTop="true"
            android:layout_centerHorizontal="true" />





        <ImageView
            android:id="@+id/statsTrophy"

            app:srcCompat="@drawable/statsoftrophy"
            android:layout_width="50dp"
            android:layout_height="50dp"
            android:layout_alignTop="@+id/textHighScore"
            android:layout_above="@+id/tryAgain_button"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true" />

        <ImageView
            android:id="@+id/trophy1"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_below="@+id/tryAgain_button"
            android:layout_marginLeft="26dp"
            android:layout_marginStart="26dp"
            android:layout_marginTop="70dp"
            android:visibility="invisible"
            app:srcCompat="@drawable/trophy1" />

        <ImageView
            android:id="@+id/trophy2"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignTop="@+id/trophy1"
            android:layout_marginLeft="24dp"
            android:layout_marginStart="24dp"
            android:layout_toEndOf="@+id/trophy1"
            android:layout_toRightOf="@+id/trophy1"
            android:visibility="invisible"
            app:srcCompat="@drawable/trophy2" />

        <Button
            android:id="@+id/enablePOPUP"
            android:layout_width="80dp"
            android:layout_height="30dp"
            android:text="Info"
            android:alpha="0.30"
            android:visibility="invisible"
            android:background="@drawable/bg_pill_trophy"
            android:textColor="#ffffff"
            android:layout_alignTop="@+id/enablePOPUP2"
            android:layout_alignLeft="@+id/trophy1"
            android:layout_alignStart="@+id/trophy1" />

        <Button
            android:id="@+id/enablePOPUP2"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:background="@drawable/bg_pill_trophy"
            android:text="Info"
            android:textColor="#ffffff"
            android:alpha="0.30"
            android:visibility="invisible"
            android:layout_marginTop="12dp"
            android:layout_below="@+id/trophy2"
            android:layout_alignLeft="@+id/trophy2"
            android:layout_alignStart="@+id/trophy2"
            android:layout_alignRight="@+id/trophy2"
            android:layout_alignEnd="@+id/trophy2" />

        <ImageView
            android:id="@+id/trophy3"
            android:layout_width="80dp"
            android:layout_height="80dp"
            android:layout_alignTop="@+id/trophy2"
            android:layout_toLeftOf="@+id/statsTrophy"
            android:layout_toStartOf="@+id/statsTrophy"
            app:srcCompat="@drawable/trophy3"
            android:visibility="invisible"/>

        <Button
            android:id="@+id/enablePOPUP3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:background="@drawable/bg_pill_trophy"
            android:text="Info"
            android:textColor="#ffffff"
            android:alpha="0.30"
            android:visibility="invisible"
            android:layout_alignBottom="@+id/enablePOPUP2"
            android:layout_alignLeft="@+id/trophy3"
            android:layout_alignStart="@+id/trophy3"
            android:layout_alignTop="@+id/enablePOPUP2"
            android:layout_alignRight="@+id/trophy3"
            android:layout_alignEnd="@+id/trophy3" />


    </RelativeLayout>




</LinearLayout>


<android.support.design.widget.NavigationView
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    app:menu="@menu/navigation_menu2"
    android:layout_gravity="start"
    android:id="@+id/nv2"
    app:headerLayout="@layout/navigation_header"
    app:itemIconTint="@drawable/tint_color_selector2">




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

1 Answer 1

1

Try to change toogle initialization. May be it helps you.

mToggle = new ActionBarDrawerToggle(this, mDrawerLayout3, mToolbar, R.string.open, R.string.close);

Edit 1

I figured out your problem. When you call getSupportActionBar you will get actionBar you recently set. So,that means your toolbar is null. Check that your toolbar id is correct.

P.S. Also, you can simply call

ActionBar actionBar = setSupportActionBar(mToolbar); 

because setSupportActionBar() returns ActionBar

Edit 2

You don't have a toolbar in your xml file. Add it.

Sign up to request clarification or add additional context in comments.

12 Comments

Still same problem: void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean) null exception
does not work either, its very strange because I a have the sam code for menu2 there i can switch to QuizActivity (Main activity) with no problems... i think there is something with the action bar (I set up in styles app theme: ....no actionbar...
@Alan I rewrote answer, check it (check that toolbar id is correct and that it exists in your menu3 layout)
I already done that.. doesn`t work (the edit one) It says required android.app.action bar but found void
@Alan is the id correct and toolbar exists in xml file?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.