ClassCastException异常的LinearLayout的LayoutParams [英] ClassCastException LinearLayout LayoutParams

查看:350
本文介绍了ClassCastException异常的LinearLayout的LayoutParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我得到了一个动作条和一些碎片。问题是我的主屏幕。上有左侧的概述片段和右侧的细节片段。我TabListener是动态的,所以我希望通过编程设置我的布局的重量。每当我试图让我的LinearLayout的的LayoutParams我得到一个ClassCastException:

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

这是夸大的布局:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:方向=垂直
    机器人:ID =@ + ID / container_found_list_overview>

    < EditText上机器人:ID =@ + ID / search_box
        机器人:layout_width =FILL_PARENT
        机器人:layout_height =50dp
        机器人:填充=5DP
        机器人:TEXTSIZE =35SP
        机器人:提示=@字符串/搜索
        机器人:inputType =文本
        机器人:MAXLINES =1/>
    <的ListView
        机器人:layout_width =match_parent
        机器人:layout_height =match_parent
        机器人:ID =@ + ID / overview_list_view>
    < / ListView控件>

< / LinearLayout中>
 

在code:

 
            的LinearLayout MyView的=(的LinearLayout)this.getView()findViewById(R.id.container_found_list_overview)。
            Log.d(发现了集装箱,+(MyView的= NULL)!);
            尝试{
                的LayoutParams LP =(的LayoutParams)myView.getLayoutParams();
                lp.weight = 1;
                lp.width = 0;
                myView.setLayoutParams(LP);
            }
            赶上(RuntimeException的E){
                Log.e(不能得到的LayoutParams,e.getMessage());
                e.printStackTrace();
            }
 

解决方案

这是怎么回事是一个观点的的LayoutParams实际上的的的类型。在这种情况下你得到一个的FrameLayout $的LayoutParams ,因为你的LinearLayout是的FrameLayout里面。

为什么父的类型?这是因为它们存储的信息是非常有用的父母打下出来。如果您检索的布局参数的EditText或ListView您将获得的LinearLayout $的LayoutParams

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

解决方案

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.

这篇关于ClassCastException异常的LinearLayout的LayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆