【问题标题】:ClassCastException when adding TextView to LinearLayout将 TextView 添加到 LinearLayout 时出现 ClassCastException
【发布时间】:2012-03-11 21:09:09
【问题描述】:

我想以编程方式向LinearLayout 添加一些TextViews。我想使用LayoutInflater。我的活动布局 xml 文件中有:

<LinearLayout
     android:id="@+id/linear_layout"
     android:layout_width="wrap_content"
     android:layout_height="fill_parent"
     android:orientation="vertical"
     />

我在下面这样写了活动代码。

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
LayoutInflater inflater = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true);
textView.setText("Some text");
linearLayout.addView(textView);

我的scale.xml 文件如下所示:

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     android:layout_weight="1"
     android:layout_marginLeft="50dp"
     android:layout_marginRight="50dp"  
     android:drawableTop="@drawable/unit"
     />

TextView textView = (TextView) inflater.inflate(R.layout.scale, linearLayout, true); 行,我遇到如下致命异常。

 java.lang.RuntimeException: Unable to start activity ComponentInfo{my.package/my.package.MyActivity}: 
 java.lang.ClassCastException: android.widget.LinearLayout
 Caused by: java.lang.ClassCastException: android.widget.LinearLayout

当我将有问题的行中的linearLayout 替换为null 时,我没有任何异常,但是我的scale.xml 中的android:layout_marginLeftandroid:layout_marginRight 被忽略了,我看不到添加的TextView 周围的任何边距。

我发现了问题Android: ClassCastException when adding a header view to ExpandableListView,但就我而言,我在使用充气机的第一行有异常。

【问题讨论】:

    标签: android textview android-linearlayout layout-inflater


    【解决方案1】:

    当您在对inflater.inflate() 的调用中指定根视图 (linearLayout) 时,膨胀的视图会自动添加到视图层次结构中。因此,您无需致电addView。此外,正如您所注意到的,返回的视图是层次结构的根视图(LinearLayout)。要获得对 TextView 本身的引用,您可以使用以下命令检索它:

    LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linear_layout);
    LayoutInflater inflater = (LayoutInflater) getApplicationContext().
        getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
    inflater.inflate(R.layout.scale, linearLayout, true);
    TextView textView = (TextView) linearLayout.getChildAt(
        linearLayout.getChildCount()-1);
    textView.setText("Some text");
    

    如果您要在 scale.xml 中为视图提供android:id 属性,您可以使用

    TextView textView = (TextView) linearLayout.findViewById(R.id.text_id);
    

    【讨论】:

    • 谢谢,但我不明白。我的LinearLayout 在布局文件中没有任何子视图。在这种情况下如何使用getChildAt 方法?当我尝试使用inflater.inflate()LinearLayout 作为我的ViewGroup 时,我遇到了异常。当我将null 用作ViewGroup 时,我的应用程序可以正常工作,但在这种情况下,我再次无法使用getChildAt 方法。
    • @woyaru - 在inflater.inflate 返回后,膨胀的TextView 将被添加到linearLayout。异常即将到来,因为您试图将返回值强制转换为 TextView,而实际上它本身正在返回 linearLayout。 (inflater.inflate 仅在根视图为null 时返回膨胀视图。如果不是null,则返回根视图,而不是膨胀的视图。)
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-03-13
    • 2011-05-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多