在制作侧边栏的过程中,我希望能够有向右滑动出现侧边栏,点击按钮也能够出现侧边栏
于是通过网络上查找代码和资料,实现了滑动出现侧边栏
但是在制作点击出现侧边栏的时候却遇到了问题——点击按钮的时候程序错误退出了,提示如下
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.support.v4.widget.DrawerLayout.openDrawer(int)' on a null object reference
也就是说获取了一个空指针,然后我加了一个
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);/*重点,获取主界面的布局,因为没有这句话我才报错*/
我的代码如下:
activity_main.xml
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
/*主页面布局,省略*/
</RelativeLayout>
/*下面是我的左侧边栏布局*/
<RelativeLayout
android:id="@+id/left"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="left" /*重点,只有设置了这个属性之后才能识别是向左弹出还是向右弹出*/
android:background="@android:color/white" >
<ListView
android:id="@+id/left_listview"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
<RelativeLayout
/*右侧边栏,省略*/
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
MainActivity.java(请直接看我做注释的重点)
public class MainActivity extends AppCompatActivity {
private List<ContentModel> list = new ArrayList<ContentModel>();
DrawerLayout drawerLayout;/*重点,声明DrawerLayout*/
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = findViewById(R.id.left_listview);
initData();
ContentAdapter adapter = new ContentAdapter(this, list);
listView.setAdapter(adapter);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);/*重点,获取主界面的布局,因为没有这句话我才报错*/
ImageView leftMenu = findViewById(R.id.leftmenu);/*重点,获取点击弹出侧边栏的组件*/
leftMenu.setOnClickListener(new OnClickListener() {/*重点,点击监听*/
@Override
public void onClick(View v) {
drawerLayout.openDrawer(Gravity.LEFT);/*重点,LEFT是xml布局文件中侧边栏布局所设置的方向*/
}
});
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id) {
switch ((int) id) {
case 1:
TextView tv=findViewById(R.id.theText);
tv.setText("dianjile");
break;
case 2:
TextView tv1=findViewById(R.id.theText);
tv1.setText("dianjile");
break;
default:
break;
}
drawerLayout.closeDrawer(Gravity.LEFT);/*重点,自动关闭侧边栏*/
}
});
}
private void initData() {
list = new ArrayList<ContentModel>();
list.add(new ContentModel(R.mipmap.lv, "新闻", 1));
list.add(new ContentModel(R.mipmap.lv, "订阅", 2));
list.add(new ContentModel(R.mipmap.lv, "图片", 3));
list.add(new ContentModel(R.mipmap.lv, "视频", 4));
list.add(new ContentModel(R.mipmap.lv, "跟帖", 5));
list.add(new ContentModel(R.mipmap.lv, "投票", 6));
}
}
关于openDrawer和closeDrawer使用的具体详解
源自:https://www.cnblogs.com/Cc1231/p/5208444.html
如下代码
DrawerLayout mdrawerLayout;
Button btn;
btn.setOnClickListener(new OnClickListener(){
mdrawerLayout.openDrawer(Gravity.LEFT);//设置的方向应该跟xml文件里gravity相同,start和LEFT都为从左边出现
mdrawerLayout.closDrawers();//没有参数,关闭所有的出现的抽屉
});
drawerLayout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/dl_left"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!--主界面剩余的布局将在以下进行-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World"/>
</LinearLayout>
<!--抽屉拉开后的布局,即ListView所在的布局-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical"
android:gravity="center"
android:layout_gravity="start">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/lv_left_menu"
android:divider="@null">
</ListView>
</LinearLayout>
</android.support.v4.widget.DrawerLayout>