我想在我的应用中导入导航抽屉。我在片段中编码,但我的应用程序在启动时崩溃,我收到以下错误:
错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView $ OnNavigationItemSelectedListener)' p>
代码:
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.support.v4.app.FragmentManager;
import android.support.v4.view.GravityCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.MenuItem;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarDrawerToggle;
public class NavigationDrawer extends AppCompatActivity {
private DrawerLayout NavDrawer;
private Toolbar toolbar;
private ActionBarDrawerToggle setupDrawerToggle() {
return new ActionBarDrawerToggle(this, NavDrawer, toolbar, R.string.drawer_open, R.string.drawer_close);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView);
setupDrawerContent(nvDrawer);
ActionBarDrawerToggle toggle = setupDrawerToggle();
NavDrawer.addDrawerListener(toggle);
}
private void setupDrawerContent(NavigationView navigationView) {
navigationView.setNavigationItemSelectedListener(
new NavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
selectDrawerItem(menuItem);
return true;
}
});
}
}
我在代码中看不到错误...
//编辑以获得更好的信息...
好的,我已经做到了,但现在出现了一个新错误......
java.lang.NullPointerException:尝试在空对象引用上调用虚方法'android.view.Window $ Callback android.view.Window.getCallback()'
但我不知道是谁的代码......
这里是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">
<include
layout="@layout/app_bar_navigation_drawer"
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_drawer"
app:menu="@menu/activity_navigation_drawer_drawer" />
</android.support.v4.widget.DrawerLayout>
//编辑确定我已经制作了......而错误
错误:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.support.design.widget.NavigationView.setNavigationItemSelectedListener(android.support.design.widget.NavigationView $ OnNavigationItemSelectedListener)' p>
已经......
public class NavigationDrawer extends AppCompatActivity {
private DrawerLayout NavDrawer;
private Toolbar toolbar;
private ActionBarDrawerToggle toggle;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_navigation_drawer);
toggle = new ActionBarDrawerToggle(this, NavDrawer,
toolbar, R.string.drawer_open, R.string.drawer_close) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
invalidateOptionsMenu();
}
};
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
NavDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView nvDrawer = (NavigationView) findViewById(R.id.nvView);
NavDrawer.setDrawerListener(toggle);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case android.R.id.home:
NavDrawer.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
toggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
toggle.onConfigurationChanged(newConfig);
}
private void selectDrawerItem(MenuItem menuItem) {
Fragment fragment = null;
Class fragmentClass;
switch(menuItem.getItemId()) {
case R.id.nav_home:
fragmentClass = Home.class;
break;
case R.id.nav_artikel:
fragmentClass = Artikel.class;
break;
case R.id.nav_facebook:
fragmentClass = Facebook.class;
break;
case R.id.nav_instagram:
fragmentClass = Instagram.class;
break;
case R.id.nav_twitter:
fragmentClass = Twitter.class;
break;
case R.id.nav_manage:
fragmentClass = Settings.class;
break;
case R.id.nav_share:
fragmentClass = Share.class;
break;
case R.id.nav_send:
fragmentClass = Send.class;
break;
default:
fragmentClass = Home.class;
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
menuItem.setChecked(true);
setTitle(menuItem.getTitle());
NavDrawer.closeDrawers();
}
}
这是Completted Navigation java ....
答案 0 :(得分:0)
您的问题似乎是您没有从setupDrawerContent()
返回任何内容。因此,当您指定ActionBarDrawerToggle toggle = setupDrawerToggle;
toggle
时,将为空值。然后,当您尝试addDrawerListener
时,您将尝试设置NullPointerException以尝试设置为null的抽屉侦听器。
以下是Google's Creating a Navigation Drawer中用于正确设置抽屉监听器的示例。
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};