嗨,大家好我是Android的新手,我正处于学习阶段。我从androidhive开始关注导航抽屉教程,并使该代码向后兼容2.3.3。 但我得到的错误如下:
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.assignment.drawersam/com.assignment.drawersam.MainDrawerActivity}: java.lang.IllegalArgumentException: View android.widget.ListView{4170e148 VFED.VC. ......I. 0,0-0,0 #7f05003e app:id/list_slidermenu} is not a sliding drawer
我无法理解错误是什么以及如何纠正错误。 任何帮助将不胜感激。
onCreate()中的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_drawer);
mTitle = mDrawerTitle = getTitle();
// load slide menu items
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
// nav drawer icons from resources
navMenuIcons = getResources()
.obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItem = new ArrayList<NavDrawerItem>();
// adding nav drawer items to array
// Home
navDrawerItem.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
// Find People
navDrawerItem.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
// Recycle the typed array
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
// setting the nav drawer list adapter
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItem);
mDrawerList.setAdapter(adapter);
// enabling action bar app icon and behaving it as toggle button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer, //nav menu toggle icon
R.string.app_name, // nav drawer open - description for accessibility
R.string.app_name // nav drawer close - description for accessibility
) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
// calling onPrepareOptionsMenu() to show action bar icons
supportInvalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
// calling onPrepareOptionsMenu() to hide action bar icons
supportInvalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
// on first time display view for first nav item
displayView(0);
}
}
activity_main_drawer.xml中的代码:
<LinearLayout 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=".MainDrawerActivity" >
<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" >
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#fff"
android:choiceMode="singleChoice"
android:divider="#369"
android:dividerHeight="1dp"
android:gravity="start"
android:listSelector="@drawable/list_selector" />
</android.support.v4.widget.DrawerLayout>
</LinearLayout>
答案 0 :(得分:5)
如documentation中所述,DrawerLayout必须是布局的根视图。
删除LinearLayout并让DrawerLayout成为根视图。
答案 1 :(得分:0)
我已经开发了与早期版本的android(Api等级7及以上版本)兼容的版本,你需要android支持库v7才能使它工作。这是完整的代码:Sliding menu based on androidhive code for api level 7 and above