ImageView单击上的导航抽屉失败,出现NullPointerException

时间:2018-02-20 18:28:13

标签: android android-studio

我希望我的导航抽屉在ImageView点击时打开,因为我禁用了应用栏,但是当我点击图片时它会崩溃我的应用。我收到NullPointerException错误,但我不知道为什么......:/

错误

  

java.lang.NullPointerException:尝试调用虚方法' void   android.support.v4.widget.DrawerLayout.openDrawer(INT)'在null   对象参考   lt.test.test.HomeActivity $ 3.onClick(HomeActivity.java:85)

第85行:

mDrawerLayout.openDrawer(GravityCompat.START);

HomeActivity.java,是我希望导航抽屉打开的地方:

public class HomeActivity extends NavigationActivity {
    DrawerLayout mDrawerLayout;

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

        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        ImageView icnMenu = findViewById(R.id.icon_menu);

        icnMenu.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                mDrawerLayout.openDrawer(GravityCompat.START); //error,  line 85
            }
        });
    }
}

NavigationActivity.java

public class NavigationActivity extends AppCompatActivity
    implements NavigationView.OnNavigationItemSelectedListener {

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

        DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        NavigationView navigationView = (NavigationView) findViewById(R.id.nav_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();
        }
    }

    @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;
    }
}

activity_navigation.xml

<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">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header_navigation"
        app:menu="@menu/activity_navigation_drawer" />

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

2 个答案:

答案 0 :(得分:2)

问题是您的mDrawerLayout在此设置为null:

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

当您的布局R.layout.activity_home不包含它时,会发生这种情况。

也许您可以执行以下操作:

setContentView(R.layout.activity_navigation);

答案 1 :(得分:1)

它为您提供NPE,因为您正在使用不同的内容视图

 setContentView(R.layout.activity_home); 

,您的抽屉位于activity_navigation.xml

您需要在父活动中包含您的变量。不要创建新变量。从父活动公开变量来访问它们。并且不要在drawer

中初始化NavActivity三次
public class HomeActivity extends NavigationActivity {

//DrawerLayout mDrawerLayout; don't create this

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

    ImageView icnMenu = findViewById(R.id.icon_menu);

    icnMenu.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            drawer.openDrawer(GravityCompat.START); //use drawer from parent activity
        }
    });
}