Xaloju Xaloju - 3 months ago 18
Android Question

How to solve "RelativeLayout cannot be cast to a DrawerLayout" error?

I'm trying to add a header image or icons in my NavigationDrawer, but all the examples I found/tried aren't useful for my implementation.

Tried to change the drawerlayout to relativeLayout onOptionsItemSelected.

@Override
public boolean onOptionsItemSelected(MenuItem item){
int id = item.getItemId();

if(id == android.R.id.home){
if(drawerLayout.isDrawerOpen(navList)){
drawerLayout.closeDrawer(navList);
}
else
drawerLayout.openDrawer(navList);
}
return super.onOptionsItemSelected(item);
}
@Override
public void onItemClick(AdapterView<?> parent,View view,int position, long id) {
loadSelection(position);
drawerLayout.closeDrawer(Gravity.LEFT);
}


Error

java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:1415)
at android.support.v4.widget.DrawerLayout.isDrawerOpen(DrawerLayout.java:1677)
at acr.acr_app.MenuPrincipalActivity.onOptionsItemSelected(MenuPrincipalActivity.java:161)
at android.app.Activity.onMenuItemSelected(Activity.java:2925)
at android.support.v4.app.FragmentActivity.onMenuItemSelected(FragmentActivity.java:403)
at android.support.v7.app.AppCompatActivity.onMenuItemSelected(AppCompatActivity.java:189)
at android.support.v7.view.WindowCallbackWrapper.onMenuItemSelected(WindowCallbackWrapper.java:100)
at android.support.v7.widget.ToolbarWidgetWrapper$1.onClick(ToolbarWidgetWrapper.java:192)
at android.view.View.performClick(View.java:4832)
at android.view.View$PerformClick.run(View.java:19844)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5319)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1016)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:811)


Updated Layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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:id="@+id/drawerlayout">
<FrameLayout
android:paddingTop="30dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/fragmentholder">
</FrameLayout>

<RelativeLayout
android:id="@+id/left_drawer"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:orientation="vertical" >

<ImageView
android:id="@+id/image_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:background="@drawable/acr_home"/>

<ListView
android:layout_width="230dp"
android:layout_height="match_parent"
android:paddingTop="80dp"
android:id="@+id/navList"
android:background="@drawable/mb04"
android:layout_gravity="start">
</ListView>
</RelativeLayout>

</android.support.v4.widget.DrawerLayout>


Activity_main

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu_principal);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerlayout);
navList = (ListView)findViewById(R.id.navList);
navList.setOnItemClickListener(this);
navList.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
ArrayList<String> navArray = new ArrayList<String>();
navArray.add("Home");
navArray.add("Frag2");
navArray.add("Frag3");
navArray.add("Frag4");
navArray.add("Frag5");

(this,android.R.layout.simple_list_item_1,navArray);
ArrayAdapter<String> adapter = new ArrayAdapter<String> (this,R.layout.my_text_view,navArray);
navList.setAdapter(adapter);
actionBarDrawerToogle = new ActionBarDrawerToggle(this,drawerLayout,R.string.opendrawer,R.string.closedrawer);
drawerLayout.addDrawerListener(actionBarDrawerToogle);
actionBar = getSupportActionBar();
fragmentManager = getSupportFragmentManager();

loadSelection(0);
}

Answer

The problem was that I didn't changed correctly which layout should be closed onClick.

    @Override
public boolean onOptionsItemSelected(MenuItem item){
    int id = item.getItemId();
    if(id == android.R.id.home){
        if(drawerLayout.isDrawerOpen(mDrawerRelativeLayout)){
            drawerLayout.closeDrawer(mDrawerRelativeLayout);
        }
        else
        drawerLayout.openDrawer(mDrawerRelativeLayout);
    }
    return super.onOptionsItemSelected(item);
}

LAYOUT

<android.support.v4.widget.DrawerLayout 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:background="#e6e6ff"
android:id="@+id/drawerlayout">
<FrameLayout
    android:paddingTop="30dp"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/fragmentholder">
</FrameLayout>

<RelativeLayout
    android:id="@+id/left_drawer"
    android:layout_width="240dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:background="#ffffff"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/image_view"
        android:layout_width="match_parent"
        android:layout_height="130dp"
        android:background="@drawable/logodrawerlateral" />

    <ListView
        android:id="@+id/navList"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:layout_below="@id/image_view"
        android:choiceMode="singleChoice" />
</RelativeLayout>