我正在学习如何使用Android的导航抽屉。我在布局xml文件中创建了一个relativeLayout,并将两个部分(FrameLayout和Listview)放在RelativeLayout中。 但我收到以下错误:
java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.slidingtest / com.slidingmenu.MainActivity}:java.lang.IllegalArgumentException:查看android.widget.RelativeLayout@423c0dc0不是滑动抽屉
我找到了这个网页,但仍无法解决我的问题。有人可以帮帮我吗?谢谢......
这是我的 MainActivity.java
public class MainActivity extends Activity {
private DrawerLayout mDrawerLayout;
private RelativeLayout relativeLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private String[] navMenuTitles;
private TypedArray navMenuIcons;
private ArrayList<NavDrawerItem> navDrawerItems;
private NavDrawerListAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
relativeLayout = (RelativeLayout)findViewById(R.id.sv_left_menu);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.list_slidermenu);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(1, -1)));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(),
navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.ic_drawer,
R.string.app_name,
R.string.app_name
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
displayView(position);
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
switch (item.getItemId()) {
case R.id.action_settings:
return true;
default:
return super.onOptionsItemSelected(item);
}
}
@Override
public boolean onPrepareOptionsMenu(Menu menu) {
boolean drawerOpen = mDrawerLayout.isDrawerOpen(this.relativeLayout);
menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
private void displayView(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new HomeFragment();
break;
case 1:
fragment = new FindPeopleFragment();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(relativeLayout);
} else {
Log.e("MainActivity", "Error in creating fragment");
}
}
@Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
activity_main.xml中
<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 to display Fragments -->
<FrameLayout
android:id="@+id/frame_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- Listview to display slider menu -->
<RelativeLayout
android:id="@+id/sv_left_menu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#191f23"
android:scrollbars="none" >
<FrameLayout
android:id="@+id/fl"
android:layout_width="240dp"
android:layout_height="wrap_content" >
<ImageView
android:id="@+id/iv_background"
android:layout_width="match_parent"
android:layout_height="180dp"
android:adjustViewBounds="true"
android:background="@drawable/circleimage_bg"
android:scaleType="centerCrop" />
<RelativeLayout
android:id="@+id/rl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginBottom="45dp" >
<com.sliding.navdrawer.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/profile_image"
android:layout_width="85dp"
android:layout_height="85dp"
android:layout_marginLeft="20dp"
android:src="@drawable/kaola"
app:border_color="#20ffffff"
app:border_width="1dp" />
<LinearLayout
android:id="@+id/signature_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_toRightOf="@+id/profile_image"
android:gravity="right"
android:orientation="vertical" >
<TextView
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="4dp"
android:layout_marginTop="4dp"
android:text="Kevin"
android:textColor="@android:color/white"
android:textSize="20sp" />
<TextView
android:id="@+id/level"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/name"
android:layout_marginLeft="4dp"
android:text="Stu"
android:textColor="@android:color/white"
android:textSize="16sp" />
</LinearLayout>
<View
android:layout_width="5dp"
android:layout_height="40dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="20dp"
android:background="#ff0000" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:background="@drawable/signature_bg_gradient"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#80e5e5e5" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="horizontal" >
<ImageView
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="center_vertical"
android:layout_marginLeft="22dp"
android:gravity="center_vertical"
android:scaleType="fitXY"
android:src="@drawable/signature_mark" />
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="4dp"
android:gravity="center_vertical"
android:text="Hello world!"
android:textColor="#969696"
android:textSize="12sp" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="0.5dp"
android:background="#80e5e5e5" />
</LinearLayout>
</FrameLayout>
<ListView
android:id="@+id/list_slidermenu"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_below="@+id/fl"
android:layout_gravity="start"
android:background="@color/list_background"
android:choiceMode="singleChoice"
android:divider="@color/list_divider"
android:dividerHeight="1dp"
android:listSelector="@drawable/list_selector" />
</RelativeLayout>
logcat的:
11-24 21:27:34.295: E/AndroidRuntime(5050): FATAL EXCEPTION: main
11-24 21:27:34.295: E/AndroidRuntime(5050): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.slidingtest/com.slidingmenu.MainActivity}: java.lang.IllegalArgumentException: View android.widget.RelativeLayout@423c0dc0 is not a sliding drawer
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2081)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2106)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread.access$600(ActivityThread.java:137)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1202)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.os.Handler.dispatchMessage(Handler.java:99)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.os.Looper.loop(Looper.java:137)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread.main(ActivityThread.java:4897)
11-24 21:27:34.295: E/AndroidRuntime(5050): at java.lang.reflect.Method.invokeNative(Native Method)
11-24 21:27:34.295: E/AndroidRuntime(5050): at java.lang.reflect.Method.invoke(Method.java:511)
11-24 21:27:34.295: E/AndroidRuntime(5050): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:808)
11-24 21:27:34.295: E/AndroidRuntime(5050): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:575)
11-24 21:27:34.295: E/AndroidRuntime(5050): at dalvik.system.NativeStart.main(Native Method)
11-24 21:27:34.295: E/AndroidRuntime(5050): Caused by: java.lang.IllegalArgumentException: View android.widget.RelativeLayout@423c0dc0 is not a sliding drawer
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1170)
11-24 21:27:34.295: E/AndroidRuntime(5050): at com.slidingmenu.MainActivity.displayView(MainActivity.java:155)
11-24 21:27:34.295: E/AndroidRuntime(5050): at com.slidingmenu.MainActivity.onCreate(MainActivity.java:93)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.Activity.performCreate(Activity.java:5084)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
11-24 21:27:34.295: E/AndroidRuntime(5050): at com.lbe.security.service.core.client.b.x.callActivityOnCreate(Unknown Source)
11-24 21:27:34.295: E/AndroidRuntime(5050): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2045)
11-24 21:27:34.295: E/AndroidRuntime(5050): ... 11 more
答案 0 :(得分:5)
哇!DrawerLayout要求该元素设置一个android:layout_gravity,所以我应该在RelativeLayout设置引力。
android:layout_gravity="start"