android – ClassCastException LinearLayout LayoutParams

前端之家收集整理的这篇文章主要介绍了android – ClassCastException LinearLayout LayoutParams前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我有一个Actionbar和一些片段.问题是我的主屏幕.有一个概述左侧的片段和右侧的细节片段.我的TabListener是动态的,所以我想以编程方式设置我的布局的重量.每当我尝试获取我的LinearLayout的Layoutparams我得到一个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)

膨胀的布局:

<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>

代码

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();
            }

解决方法

发生的是一个视图的LayoutParams实际上是父类的类型.在这种情况下,您将获得一个FrameLayout $LayoutParams,因为您的LinearLayout位于FrameLayout中.

为什么是父母的类型?这是因为他们存储对父母有用的信息.如果要检索EditText或ListView的布局参数,您将获得LinearLayout $LayoutParams.

原文链接:https://www.f2er.com/android/311325.html

猜你在找的Android相关文章