带有Nullpointer异常的导航抽屉/错误出错

时间:2017-07-25 13:21:56

标签: java android nullpointerexception navigation-drawer

我用3个活动编写了一个测验:1 = QuizActivity,2 = Menu2和3 = Menu3。活动1和2工作得非常好。问题出在Menu3中。有一个行代码创建一个Nullpoiner excption: void android.support.v7.app.ActionBar.setDisplayHomeAsUpEnabled(boolean)'在null对象引用当我删除这一行时,我可以启动(通过单击Navigation抽屉上的about项目(导致Menu3活动))menu3活动。唯一的问题是当我把这条线留下来时," Burger图标" (你可以在图片上看到它)不再存在了。当我离开行代码时,我无法导航到Menu3,因为当我这样做时,应用程序强制关闭。我该如何解决这个问题?

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 个答案:

答案 0 :(得分:1)

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

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

修改1

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

P.S。此外,您只需致电

ActionBar actionBar = setSupportActionBar(mToolbar); 

因为setSupportActionBar()返回ActionBar

修改2

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