我是Android新手
通过这个简单的程序,我想使用按钮onclick()打开一个抽屉。但是这给出了null异常错误。我通过谷歌搜索尝试了解决方案,但遗憾的是没有解决它。
这是 onCreate()方法
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
navListener();
}
navListener()
public void navListener(){
final DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
ImageView nav = (ImageView) findViewById(R.id.navbarimagebutton);
nav.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
mDrawerLayout.openDrawer(GravityCompat.START);
}
});
}
activity_main.xml中
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<include layout="@layout/app_bar" />
</RelativeLayout>
DrawerLayout
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_height="match_parent"
android:layout_width="120dp"
xmlns:android="http://schemas.android.com/apk/res/android">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"></ListView>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
app_bar
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/app_bar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="@color/red">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="@+id/navbarimagebutton"
android:layout_width="0dp"
android:layout_weight=".25"
android:layout_height="fill_parent"
android:src="@drawable/menu"
/>
<View
android:id="@+id/homepagefirstview"
android:layout_width="10dp"
android:layout_height="fill_parent"></View>
<ImageView
android:layout_weight="1"
android:layout_width="0dp"
android:src="@drawable/hometitle"
android:layout_height="fill_parent" />
<View
android:layout_width="10dp"
android:layout_height="fill_parent"/>
<ImageView
android:layout_weight=".25"
android:layout_width="0dp"
android:src="@drawable/search"
android:layout_height="fill_parent" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
答案 0 :(得分:1)
您的问题是您向setContentView(R.layout.activity_main);
此文件不包含您的DrawerLayout
你需要这样做:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity">
<!-- add an id here since your using RelativeLayout -->
<include layout="@layout/app_bar" android:id="@+id/app_bar" />
<!-- include the drawer this way, probably need to add layout_below="@+id/app_bar" or whatever the id is for your app bar -->
<!-- Uncomment this <include layout="@layout/drawer_layout"/> -->
<!-- Or simply copy the drawer layout xml and add layout_below="@+id/app_bar" -->
<android.support.v4.widget.DrawerLayout
android:id="@+id/drawerlayout"
android:layout_below="@+id/app_bar"
android:layout_height="match_parent"
android:layout_width="match_parent">
<!-- main content area -->
<FrameLayout
android:id="@+id/content_area"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<!-- Slide out drawer section -->
<FrameLayout
android:layout_gravity="start"
android:layout_width="320dp"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</FrameLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
您正在遇到NullPointerException,因为您的虚增视图不包含您引用的DrawerLayout