Java 尝试添加导航抽屉时出现NullPointerException(Android)
我有点卡住了。我一直在学习一些教程,最后制作了一个抽屉布局,如下所示:Java 尝试添加导航抽屉时出现NullPointerException(Android),java,android,android-layout,android-menu,android-toolbar,Java,Android,Android Layout,Android Menu,Android Toolbar,我有点卡住了。我一直在学习一些教程,最后制作了一个抽屉布局,如下所示: <?xml version="1.0" encoding="utf-8"?> <android.support.v4.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/nav_drawer" android:layout_width="match_parent" android:l
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/nav_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<ListView
android:id="@+id/list"
android:entries="@array/menuitems"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="left">
</ListView>
</android.support.v4.DrawerLayout>
但是,现在我在打开主布局时遇到运行时异常,原因是:android.view.InflateException:二进制XML文件第2行:错误膨胀类android.support.v4.DrawerLayout-然后它抱怨setContentView。。主活动中的行
我已经尝试在gradle脚本中添加依赖项
compile 'com.android.support:support-v4:20.0.0'
但它的下划线为红色,表示支持库不应使用低于targetSdkVersion 22的版本20
有什么想法吗 看起来navigation\u drawer.xml不是主布局?
您可以使用include标记并设置id。然后尝试FindViewByInClude_id.findViewByIdR.id.list
再试一次
<include layout="@layout/navigation_drawer"
android:id="@+id/navdraw"/>
compile 'com.android.support:support-v4:20.0.0'
hamburgerIcon = (ImageView) findViewById(R.id.hamburger);
mDrawerList = (ListView) findViewById(R.id.list);
hamburgerIcon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String[] osArray = { "Bla", "Bla", "alB", "alB", "BLA" };
mAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
}
};