Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

The ERROR about DrawerLayout(IllegalArgumentException: View android.widget.RelativeLayout is not a sliding drawer)

I'm learning how to use the Navigation Drawer for Android. I have created a relativeLayout in the layout xml file and placed the two parts(FrameLayout, and Listview) in the RelativeLayout. But I'm getting the following error:

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

ClassCastException android.widget.FrameLayout$LayoutParams to android.support.v4.widget.DrawerLayout$LayoutParams

I find this webpage, but still can't solve my peoblem. Anyone can help me? Thank you...

This is my 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
like image 512
Kevin.Z Avatar asked Nov 24 '14 13:11

Kevin.Z


1 Answers

Wow!The DrawerLayout requires that element to have a android:layout_gravity set on it, so I should set the gravity at RelativeLayout.

android:layout_gravity="start"
like image 124
Kevin.Z Avatar answered Oct 14 '22 22:10

Kevin.Z