我用3个 Activity 编写了一个测验:1 = QuizActivity,2 = Menu2和3 = Menu3。 Activity 1和2运行正常。问题出在Menu3中。有一行代码正在创建Nullpoiner例外:删除此行时,空对象引用上的 void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'可以启动(通过单击about项目在导航抽屉上(引导到Menu3 Activity ))。唯一的问题是,当我忽略此行时,“汉堡图标”(您可以在图片上看到)不再存在。当我离开代码行时,我无法导航至Menu3,因为当我这样做时,应用程序强制关闭。我怎么解决这个问题?

java - 导航抽屉错误/Nullpointer异常错误-LMLPHP

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>

最佳答案

尝试更改toogle初始化。可能对您有帮助。

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

编辑1

我知道了你的问题。您打电话的时候
getSupportActionBar您将获得最近设置的actionBar。因此,这意味着您的工具栏为空。检查您的工具栏ID是否正确。

附言另外,您只需拨打
ActionBar actionBar = setSupportActionBar(mToolbar);

因为setSupportActionBar()返回ActionBar

编辑2

您的xml文件中没有工具栏。添加它。

08-04 05:00