我正在尝试为从右侧打开的导航抽屉上显示的某些操作设置列表视图。该项目将编译没有错误,我在已经在视图中的ListView上尝试了我的BaseAdapter,并且工作正常。我的应用程序中没有操作栏,因此用户必须通过从屏幕右边缘滑动来打开抽屉。列表视图中显示的所有信息都在适配器中处理。
以下是onCreate
中与抽屉相关的内容(在官方教程中,他们参考实际DrawerLayout的唯一原因是在操作栏上给它一个切换,我不知道有):
当我尝试分配适配器时,我现在得到一个空指针异常。
ListView drawerList = (ListView) findViewById(R.id.drawerList);
返回null
更新代码
ListView drawerList = (ListView) findViewById(R.id.drawerList);
SectionedAdapter DrawerAdapter = new SectionedAdapter(this);
drawerList.setAdapter(DrawerAdapter);
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/left_drawer">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawer_frame">
<AllOfMyStuff/>
<FrameLayout/>
<!--Always returns null-->
<ListView android:id="@+id/drawerList"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#ff1f1f1f"
android:drawSelectorOnTop="true"/>
</android.support.v4.widget.DrawerLayout>
答案 0 :(得分:1)
你的xml似乎很好,但是你在片段中而不是在一个Activity上夸大了menu_drawer.xml。查看这个视频以获得更好的想法:: https://www.youtube.com/watch?v=K8hSIP2ha-g
这是我的代码..
activity_main.xml中
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/drawerlayout">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/maincontent"></FrameLayout>
<ListView
android:background="#FFFFFF"
android:divider="@null"
android:layout_width="240dp"
android:layout_height="match_parent"
android:id="@+id/drawerlist"
android:layout_gravity="left">
</ListView>
</android.support.v4.widget.DrawerLayout>
MainActivity.java
package com.example.nav;
import android.app.ActionBar;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends Activity {
DrawerLayout mdrawer;
private ListView list;
private String[] planets;
ActionBar action;
ActionBarDrawerToggle mtoggle;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
list=(ListView)findViewById(R.id.drawerlist);
mdrawer=(DrawerLayout)findViewById(R.id.drawerlayout);
action=getActionBar();
mtoggle=new ActionBarDrawerToggle(this, mdrawer, R.drawable.ic_drawer,R.string.OndrawerOpen,R.string.OndrawerClose){
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
@Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
};
mdrawer.setDrawerListener(mtoggle);
getActionBar().setHomeButtonEnabled(true);
getActionBar().setDisplayHomeAsUpEnabled(true);
planets=getResources().getStringArray(R.array.planets);
list.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,planets));
list.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,long arg3) {
setTitle(planets[position]);
}
});
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(mtoggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mtoggle.syncState();
}
void setTitle(String value){
getActionBar().setTitle(value);
}
}
这可能会有所帮助!!
答案 1 :(得分:0)
我找到了解决问题的方法:
1 /它应该设置
android:theme="@style/AppThemeNoBar"
清单中的样式。 添加样式
<style name="AppThemeNoBar" parent="Theme.AppCompat.Light">
<item name="android:windowNoTitle">true</item>
</style>
2 / menu_drawer:
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- The main content view -->
<RelativeLayout
android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</RelativeLayout>
<!-- The navigation drawer -->
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/></android.support.v4.widget.DrawerLayout>
3 / MainActivity:
DrawerLayout mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerToggle = new ActionBarDrawerToggle(
this, mDrawerLayout, R.drawable.menu_icon, R.string.drawer_open,
R.string.drawer_close
) {
@Override
public void onDrawerClosed(View view) {
invalidateOptionsMenu();
}
@Override
public void onDrawerOpened(View drawerView) {
invalidateOptionsMenu();
}
};
结果你得到一个没有ActionBar的滑动菜单。
答案 2 :(得分:0)
也许你在视图ID名称中犯了一个错误:
findViewById(R.id.drawList);
应该是R.id.drawerList
而不是R.id.drawList
。
P.S。我没有足够的评分来回答评论。