ClassCastException android.widget.FrameLayout$LayoutParams to android.support.v4.widget.DrawerLayout$LayoutParams
Asked Answered
T

8

39

I'm working on the Navigation Drawer for Android. As per my requirement I was to display gridview and listview of items in the navigation drawer. I have created a linearLayout in the layout xml file and placed the two widgets(Grid view, and Listview) in the LinearLayout.

When I run the file I'm getting the following error:

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer3/com.example.navigationdrawer3.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams

Below are my java, logcat, and NavigationDrawer layout files:

MainActivity.java

public class MainActivity extends Activity {

     private DrawerLayout mDrawerLayout;
        private ListView mDrawerList;
        private ActionBarDrawerToggle mDrawerToggle;

        private CharSequence mDrawerTitle;
        //@SuppressWarnings("unused")
        private CharSequence mTitle;
        private String[] mGalaxyTitles;

        private GridView mDrawerGrid;

        private LinearLayout mDrawerLinear;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mTitle = mDrawerTitle = getTitle();
        mGalaxyTitles = getResources().getStringArray(R.array.items_array);
        mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
        mDrawerList = (ListView) findViewById(R.id.left_drawer);

        mDrawerGrid = (GridView)findViewById(R.id.gridview);

        mDrawerLinear =(LinearLayout)findViewById(R.id.linearLayout);

        mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        mDrawerGrid.setAdapter(new ImageAdapter(MainActivity.this));

        mDrawerList.setAdapter(new ArrayAdapter<String>(this,
                R.layout.drawer_list_item, mGalaxyTitles));

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

        getActionBar().setDisplayHomeAsUpEnabled(true);
        getActionBar().setHomeButtonEnabled(true);      

        mDrawerToggle = new ActionBarDrawerToggle(
                this,                  
                mDrawerLayout,         
                R.drawable.ic_drawer,  
                R.string.drawer_open,  
                R.string.drawer_close  
                ) {
            public void onDrawerClosed(View view) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); 
            }

            public void onDrawerOpened(View drawerView) {
                getActionBar().setTitle(mDrawerTitle);
                invalidateOptionsMenu(); 
            }
        };
        mDrawerLayout.setDrawerListener(mDrawerToggle);

        if (savedInstanceState == null) {
            selectItem(0);
        }

    }

    class DrawerItemClickListener implements ListView.OnItemClickListener {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position,
                long id) {
            // TODO Auto-generated method stub

switch(position){

            case 0:
               menu0(); 
                return;
            case 1:
               menu1(); 
                return;
            case 2:
                menu2(); 
                 return;
            case 3:
               menu3(); 
                return;
            case 4:
               menu4(); 
                return;
            case 5:
               menu5(); 
                return;
            case 6:
               menu6(); 
                return;
            case 7:
               menu7(); 
                return;
            case 8:
               menu8(); 
                return;
        }

        }


        protected void menu0() {
            Intent Main0 = new Intent(MainActivity.this, Page0.class);
            startActivity(Main0);
               return;
        }

        protected void menu1() {
            Intent Main1 = new Intent(MainActivity.this, Page1.class);
            startActivity(Main1);
               return;
        }

        protected void menu2() {
              Intent Main2 = new Intent(MainActivity.this, Page2.class);
              startActivity(Main2);
           return;
    }
        protected void menu3() {
            Intent Main3 = new Intent(MainActivity.this, Page3.class);
            startActivity(Main3);
               return;
        }

        protected void menu4() {
            Intent Main4 = new Intent(MainActivity.this, Page4.class);
            startActivity(Main4);
               return;
        }

        protected void menu5() {
            Intent Main5 = new Intent(MainActivity.this, Page5.class);
            startActivity(Main5);
               return;
        }

        protected void menu6() {
            Intent Main6 = new Intent(MainActivity.this, Page6.class);
            startActivity(Main6);
               return;
        }

        protected void menu7() {
            Intent Main7 = new Intent(MainActivity.this, Page7.class);
            startActivity(Main7);
               return;
        }

        protected void menu8() {
            Intent Main8 = new Intent(MainActivity.this, Page8.class);
            startActivity(Main8);
               return;
        }

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }


     @Override
        public void setTitle(CharSequence title) {
            mTitle = title;
            getActionBar().setTitle(mDrawerTitle);
        }

       @Override
        public boolean onPrepareOptionsMenu(Menu menu) {
           //boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
           //boolean drawerOpen2 = mDrawerLayout.isDrawerOpen(mDrawerGrid);
           boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerLayout);
            menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            //menu.findItem(R.id.action_settings).setVisible(!drawerOpen2);
            return super.onPrepareOptionsMenu(menu);
        }

       @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }

            switch(item.getItemId()) {
            case R.id.action_settings:
                setting();

                return true;

            case R.id.about:
                about();

                return true;
            default:
                return super.onOptionsItemSelected(item);
            }
        }

     private void about() {
        // TODO Auto-generated method stub

         Intent Main1 = new Intent(MainActivity.this, About.class);
            startActivity(Main1);
               return;

    }


    private void setting() {
        // TODO Auto-generated method stub
        Intent Main1 = new Intent(MainActivity.this, Setting.class);
        startActivity(Main1);
           return;

    }


    private void selectItem(int position) {


            Fragment fragment = new GalaxyFragment();
            Bundle args = new Bundle();
            args.putInt(GalaxyFragment.ARG_GALAXY_NUMBER, position);
            fragment.setArguments(args);

            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();


            mDrawerList.setItemChecked(position, true);
            setTitle(mGalaxyTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerLayout);
            //mDrawerLayout.closeDrawer(mDrawerGrid);
        }

     class GalaxyFragment extends Fragment{

        public static final String ARG_GALAXY_NUMBER = "galaxy_number";

         public GalaxyFragment() {

            }

            @Override
            public View onCreateView(LayoutInflater inflater, ViewGroup container,
                    Bundle savedInstanceState) {
                View rootView = inflater.inflate(R.layout.about, container, false);           

                return rootView;
            }

     }


     //Gridview BaseAdapter class

     class ImageAdapter extends BaseAdapter{
            Context context;

        ImageAdapter(Context context){
                this.context = context;
            }


            @Override
            public int getCount() {
                // TODO Auto-generated method stub
                return mThumbIds.length;
            }

            @Override
            public Object getItem(int arg0) {
                // TODO Auto-generated method stub
                return null;
            }

            @Override
            public long getItemId(int arg0) {
                // TODO Auto-generated method stub
                return 0;
            }

            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // TODO Auto-generated method stub
                ImageView imageView;

                if(convertView == null){
                    imageView = new ImageView(context);
                     imageView.setLayoutParams(new GridView.LayoutParams(25, 25));
                     imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                     imageView.setPadding(8, 8, 8, 8);
                }else {
                    imageView = (ImageView) convertView;
                }
                imageView.setImageResource(mThumbIds[position]);
                return imageView;
            }

             // references to our images
            private Integer[] mThumbIds = {
                    R.drawable.sample_2, R.drawable.sample_3,
                    R.drawable.sample_4, R.drawable.sample_5,
            };

        }

}

activity_main.xml

<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"
    tools:context=".MainActivity" 
    android:id="@+id/drawer_layout">

     <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

      <LinearLayout
         android:id="@+id/linearLayout"
         android:layout_width="320dp"
         android:layout_height="match_parent"
         android:orientation="vertical" 
         android:layout_gravity="left"
        android:choiceMode="singleChoice"
        android:divider="@android:color/transparent"
        android:dividerHeight="0dp"
        android:background="#111">        


    <GridView 
        android:id="@+id/gridview"
        android:layout_width="280dp"
        android:layout_height="match_parent"
        android:stretchMode="columnWidth"
        android:numColumns="auto_fit"/>
       <!--  android:gravity="center"   --> 
    <ListView
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        />

   </LinearLayout>     

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

logcat

07-30 06:15:57.203: W/dalvikvm(853): threadid=1: thread exiting with uncaught exception (group=0x40a71930)
07-30 06:15:57.213: E/AndroidRuntime(853): FATAL EXCEPTION: main
07-30 06:15:57.213: E/AndroidRuntime(853): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.navigationdrawer3/com.example.navigationdrawer3.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2180)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread.access$600(ActivityThread.java:141)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1234)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.os.Looper.loop(Looper.java:137)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread.main(ActivityThread.java:5041)
07-30 06:15:57.213: E/AndroidRuntime(853):  at java.lang.reflect.Method.invokeNative(Native Method)
07-30 06:15:57.213: E/AndroidRuntime(853):  at java.lang.reflect.Method.invoke(Method.java:511)
07-30 06:15:57.213: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
07-30 06:15:57.213: E/AndroidRuntime(853):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
07-30 06:15:57.213: E/AndroidRuntime(853):  at dalvik.system.NativeStart.main(Native Method)
07-30 06:15:57.213: E/AndroidRuntime(853): Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.support.v4.widget.DrawerLayout.isDrawerView(DrawerLayout.java:809)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.support.v4.widget.DrawerLayout.closeDrawer(DrawerLayout.java:1012)
07-30 06:15:57.213: E/AndroidRuntime(853):  at com.example.navigationdrawer3.MainActivity.selectItem(MainActivity.java:265)
07-30 06:15:57.213: E/AndroidRuntime(853):  at com.example.navigationdrawer3.MainActivity.onCreate(MainActivity.java:86)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.Activity.performCreate(Activity.java:5104)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1080)
07-30 06:15:57.213: E/AndroidRuntime(853):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2144)
07-30 06:15:57.213: E/AndroidRuntime(853):  ... 11 more

The line of code at line 86:

selectItem(0);

The line of code at line 265:

mDrawerLayout.closeDrawer(mDrawerLayout);

I have tried to correct this run time erroe in many ways but failed to correct. Can any one suggest a answer for my problem.

Taciturnity answered 30/7, 2013 at 6:31 Comment(12)
error suggests that you are passing frame layout to drawer layout.Gatha
Then how to rectify it. Am I supposed to remove Framelayout from the layout file?Taciturnity
try mDrawerLayout.closeDrawer(mDrawerList);Gatha
Error. But it has changed now it is displaying java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParamsTaciturnity
Have you passed the right layout for layout drawer ?? check again what it contains.Gatha
Ofcourse I feel that my xml for Layout Drawer is correct. I didn't find any errors. Please notify me if you found any....Taciturnity
I mean in CloseDrawer(mDrawerLayout). check what it contains.Gatha
Dude. Thanks for notifying me it has worked.Taciturnity
But now the grid view displaying but list view is not displaying items?Taciturnity
Check with data is there in listview or not?? and see design also that listview may have dispositioned by gridview.Gatha
Ofcourse data is there in the listview....Also how can it get dispositioned with gridview...Taciturnity
If you don't have any error means that there is no data to listview or listview has not properly assigned with adapter. so i don't think there may be another issue with listview.Gatha
G
23

Answer:

mDrawerLayout.closeDrawer(mDrawerLayout);

had wrong Layout. so change in it with drawerlayout will work it out.

Gatha answered 30/7, 2013 at 7:2 Comment(7)
What do you mean "Had wrong layout" - In what way is it the wrong layout?Ledezma
it had wrong layout referenced in closeDrawer(). and answer is accepted so i think i was right.Gatha
@Ledezma +1, answer is not clear. The parameter given to closeDrawer() should be the viewgroup that corresponds to the drawer portion of the drawerlayout.Travers
That is correct. I had the same issue, because I copied the code from the Google's example. So apparently they made an error thereNuno
Same happened to me after customizing my drawer because they use lsit drawer in google but I used linear layout.Cogitate
This "answer" is useless, am sorry.Putdown
this is a poorly done answer, @piyush please provide more precise answerNyssa
S
36

What solved this issue for me:

In MainActivity, add a new field for the LinearLayout, and assign value to it in onCreate() (this part just like emaleavil suggested):

private LinearLayout linearLayout;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    // ...
    linearLayout = (LinearLayout) findViewById(R.id.linearLayout);
}

Then in selectItem(), when calling closeDrawer(), simply pass linearLayout as argument:

drawerLayout.closeDrawer(linearLayout);
Sorrel answered 30/10, 2013 at 15:36 Comment(2)
This is because the linearLayout in the question is the "drawer" described in the method description - aka, the second child of the DrawerLayoutLedezma
this is not working for me. I had this error: linearLayout is not a sliding drawerUnivocal
H
28

I had the same problem. I just got rid of using,

mDrawerLayout.closeDrawer(Gravity.LEFT);
Hardpan answered 3/7, 2014 at 17:44 Comment(0)
G
23

Answer:

mDrawerLayout.closeDrawer(mDrawerLayout);

had wrong Layout. so change in it with drawerlayout will work it out.

Gatha answered 30/7, 2013 at 7:2 Comment(7)
What do you mean "Had wrong layout" - In what way is it the wrong layout?Ledezma
it had wrong layout referenced in closeDrawer(). and answer is accepted so i think i was right.Gatha
@Ledezma +1, answer is not clear. The parameter given to closeDrawer() should be the viewgroup that corresponds to the drawer portion of the drawerlayout.Travers
That is correct. I had the same issue, because I copied the code from the Google's example. So apparently they made an error thereNuno
Same happened to me after customizing my drawer because they use lsit drawer in google but I used linear layout.Cogitate
This "answer" is useless, am sorry.Putdown
this is a poorly done answer, @piyush please provide more precise answerNyssa
G
16

I had the same issue... Solution in my case was:

In your MainActivity add:

private LinearLayout linearLayout;

public void onCreate(Bundle savedInstanceState){

    ...
linearLayout = (LinearLayout)findViewById(R.id.linearLayout);
    ...

}

OnPrepareOptionsMenu:

@Override
        public boolean onPrepareOptionsMenu(Menu menu) {
            boolean drawerOpen = mDrawerLayout.isDrawerOpen(this.linearLayout);
            menu.findItem(R.id.action_settings).setVisible(!drawerOpen);
            return super.onPrepareOptionsMenu(menu);
}

I'm sorry for my poor english.

Goblet answered 6/9, 2013 at 16:38 Comment(2)
+1, this pointed me in the right direction. Far more useful than the accepted answer. (For me onPrepareOptionsMenu was not relevant; I just had to pass linearLayout to closeDrawer.)Sorrel
@Sorrel The important part is that the linearLayout is the "drawer" - aka, the second child of the DrawerLayoutLedezma
S
2

I think you should place your content and drawer exactly as it is siad in the manual. Now yor XML structure doesn't match that. Note, that only the "id" field should match, the view types and parameters may be different.

Scarcity answered 30/7, 2013 at 6:38 Comment(7)
Have you seen Evernote for Android. They have included a gridview and listview in the DrawerLayout. If they have followed the android developer manual, then how would they got that view. Plz navigate to below link to see the image of Evernote Navigation Drawer. goo.gl/fNDm0KTaciturnity
@Taciturnity Just replace the ListView with id "@+id/left_drawer" with a RelativeLayout, for example, with the same id and add anything you want to this layout - grid or listview, or anything else. It will be shown just as on the picture you gave the link to. Also, please, consider the restrictions for content and drawer layout given in the manual - now you don't specify the gravity and width of the drawer.Scarcity
I was unable to get you clearly. Could you post me a sample code of your description. It would be helpful..Taciturnity
Hey the I have rectified the error how ever..now the listview is not displaying in the drawerlayout and only gridview is displaying. Can you please help me how can I rectify this...Taciturnity
@Taciturnity Did you make any changes in th XML? If you did, please, post the new one.Scarcity
I didn't make changes to the xml file, I have made few changes to the java code previously to make it work correctly..Taciturnity
@Taciturnity Well, then I don't know why is's happening, but maybe you can fix it by moving the GreedView and ListView to one layout and set this layout's id to "@+id/left_drawer".Scarcity
G
2

finally I found the way to implement a custom listView in the drawer layout. This is the post that help me to do it...http://www.michenux.net/android-navigation-drawer-748.html

Goblet answered 4/12, 2013 at 21:59 Comment(0)
C
2
mDrawerLayout.closeDrawer(mDrawerLayout);

or

mDrawerLayout.closeDrawer(Gravity.LEFT);

both is ok.NOTE:you must know What is parameter.

Have a nice day !

Confiscatory answered 7/3, 2017 at 2:2 Comment(1)
Please edit your answer so that the code shows up as code.Seaddon
C
0

For those who are getting this exception when using NavigationView inside the DrawerLayout and want to close the menu without showing closing animation:

call this method: drawerLayout.closeDrawer(navigationView, false)

first param: the navigationView from the inflated layout, second param: boolean to display closing animation

(Using com.google.android.material.navigation.NavigationView androidx.drawerlayout.widget.DrawerLayout)

Corr answered 27/8, 2019 at 13:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.