10

I got an Actionbar and some Fragments. The Problem is my Homescreen. There is an overview Fragment on the left side and a detail Fragment on the right side. My TabListener is Dynamic so i want to set the weight of my Layout programatically. Whenever i try to get the Layoutparams of my LinearLayout i get a ClassCastException:

12-22 16:00:37.620: W/System.err(7235): java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams
12-22 16:00:37.625: W/System.err(7235):     at com.coover.eu.lostandfound.ListSearchFragment.onViewCreated(ListSearchFragment.java:184)
12-22 16:00:37.625: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:884)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1080)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:622)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1416)
12-22 16:00:37.630: W/System.err(7235):     at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:505)
12-22 16:00:37.630: W/System.err(7235):     at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1178)
12-22 16:00:37.635: W/System.err(7235):     at android.app.Activity.performStart(Activity.java:5216)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2083)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2135)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread.access$700(ActivityThread.java:140)
12-22 16:00:37.635: W/System.err(7235):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1237)
12-22 16:00:37.640: W/System.err(7235):     at android.os.Handler.dispatchMessage(Handler.java:99)
12-22 16:00:37.640: W/System.err(7235):     at android.os.Looper.loop(Looper.java:137)
12-22 16:00:37.640: W/System.err(7235):     at android.app.ActivityThread.main(ActivityThread.java:4921)
12-22 16:00:37.640: W/System.err(7235):     at java.lang.reflect.Method.invokeNative(Native Method)
12-22 16:00:37.640: W/System.err(7235):     at java.lang.reflect.Method.invoke(Method.java:511)
12-22 16:00:37.645: W/System.err(7235):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1038)
12-22 16:00:37.645: W/System.err(7235):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:805)
12-22 16:00:37.645: W/System.err(7235):     at dalvik.system.NativeStart.main(Native Method)

The Layout that is inflated:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" 
    android:id="@+id/container_found_list_overview">

    <EditText android:id="@+id/search_box" 
        android:layout_width="fill_parent"
        android:layout_height="50dp"
        android:padding="5dp"
        android:textSize="35sp"
        android:hint="@string/search"
        android:inputType="text"
        android:maxLines="1"/>
    <ListView 
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/overview_list_view">
    </ListView>

</LinearLayout> 

The code:


            LinearLayout myView = (LinearLayout)this.getView().findViewById(R.id.container_found_list_overview);
            Log.d("found container", ""+(myView != null));
            try{
                LayoutParams lp = (LayoutParams) myView.getLayoutParams();
                lp.weight = 1;
                lp.width = 0;
                myView.setLayoutParams(lp);
            }
            catch(RuntimeException e){
                Log.e("cant get layoutparams",e.getMessage());
                e.printStackTrace();
            }
dmon
  • 30,048
  • 8
  • 87
  • 96
Ceryni
  • 371
  • 1
  • 5
  • 18
  • possible duplicate of [Getting ClassCastException when trying to insert RelativeLayout dyanmically?](http://stackoverflow.com/questions/10045360/getting-classcastexception-when-trying-to-insert-relativelayout-dyanmically) – ForceMagic Apr 23 '14 at 16:01

3 Answers3

32

What's going on is that a view's LayoutParams are actually the type of the parent. In this case you're getting a FrameLayout$LayoutParams because your LinearLayout is inside a FrameLayout.

Why is it the type of the parent? That's because they store information that's useful for the parent to lay them out. If you were to retrieve the layout parameters for the EditText or ListView you would get LinearLayout$LayoutParams.

dmon
  • 30,048
  • 8
  • 87
  • 96
  • Thank you very much it works fine now. Instead of layout_weight I use the layout_gravity and change the width dynamically – Ceryni Dec 23 '12 at 10:40
  • Ah ok thank you for your advice. My first Question at stackoverflow – Ceryni Dec 23 '12 at 22:03
  • So, what's the proper solution then? If for example I want to add a top margin to my linear layout which is nested within the relative layout? – halxinate Feb 26 '13 at 20:00
  • You should use `RelativeLayout.LayoutParams`. [Source](http://grepcode.com/file/repository.grepcode.com/java/ext/com.google.android/android/4.1.2_r1/android/widget/RelativeLayout.java#RelativeLayout.LayoutParams) – dmon Feb 26 '13 at 20:10
1

Exception clearly states that you are trying to cast FrameLayout to LinearLayout. This occurs in onViewCreated() method in ListSearchFragment.java file, line 184. So replace this:

LayoutParams lp = (LayoutParams) myView.getLayoutParams();

with this:

LinearLayout.LayoutParams lp = 
                  (LinearLayout.LayoutParams) myView.getLayoutParams();

Before you do that, you can hover your mouse pointer on LayoutParams in your current code and see in bubble that it is in fact android.widget.RelativeLayout.LayoutParams hence your exception. See:

enter image description here

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • That does not work. Exactly the same exception. Looking at the accepted answer above - it is NOT possible at all. I will be happy to know I'm wrong. – halxinate Feb 26 '13 at 20:01
0

There is another reason (or bug) for this exception:

If the view's ID is also being used in another layout, then it'll try to cast LayoutParams for the Parent views used in both layouts.

Solution: Dont use same id in different layouts :)

M. Usman Khan
  • 3,689
  • 1
  • 59
  • 69