入门java.lang.ClassCastException:android.widget.FrameLayout $的LayoutParams不能转换为android.widget.RelativeLayout $的LayoutParams [英] Getting java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams

本文介绍了入门java.lang.ClassCastException:android.widget.FrameLayout $的LayoutParams不能转换为android.widget.RelativeLayout $的LayoutParams的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在这里,我需要在运行时添加的TextView。首先我把RelativeLayout的参考拿到布局PARAMS但不幸的是在其投掷抛出ClassCastException RelativeLayout.LayoutParams PARAMS =(android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()

我也查 - <一个href=\"http://stackoverflow.com/questions/11544964/framelayout-to-relativelayout-classcastexception-even-if-there-is-no-framelayout\">FrameLayout到RelativeLayout的ClassCastException异常,即使没有使用的FrameLayout,但没有奏效。由于与更换的FrameLayout后RelativeLayout的,我无法对的LayoutParams添加规则。

 保护无效的onCreate(捆绑savedInstanceState){
            super.onCreate(savedInstanceState);
            的setContentView(R.layout.activity_main);            TextView的displayTextView =(的TextView)findViewById(R.id.display_text);            ContentResolver的解析器= getContentResolver();
            光标光标= resolver.query(UserDictionary.Words.CONTENT_URI,NULL,NULL,NULL,NULL);            cursor.moveToFirst();
            displayTextView.setText(以下简称用户词典中包含+ cursor.getColumnCount()+字);            RelativeLayout的contentLayout =(RelativeLayout的)findViewById(R.id.content_layout);            TextView的COLUMNNAMES =新的TextView(本);
            columnNames.setText(栏目:_id - 频率 - 字);
            contentLayout.addView(COLUMNNAMES);            RelativeLayout.LayoutParams PARAMS =(android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.BELOW);            做{
                TextView的入门=新的TextView(本);
                entry.setText(cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID))+ - +
                              cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY))+ - +
                              cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)));                entry.setLayoutParams(PARAMS);
                contentLayout.addView(输入);
            }而(cursor.moveToNext());            cursor.close();
    }    **获取以下错误** -
    了java.lang.RuntimeException:无法启动活动ComponentInfo {com.example.dexter.contentpro / com.example.dexter.contentpro.MainActivity}:java.lang.ClassCastException:android.widget.FrameLayout $的LayoutParams无法转换到Android。 widget.RelativeLayout $的LayoutParams
                在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
                在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
                在android.os.Handler.dispatchMessage(Handler.java:102)
                在android.os.Looper.loop(Looper.java:135)
                在android.app.ActivityThread.main(ActivityThread.java:5221)
                在java.lang.reflect.Method.invoke(本机方法)
                在java.lang.reflect.Method.invoke(Method.java:372)
                在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
                在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         java.lang.ClassCastException:引起android.widget.FrameLayout $的LayoutParams不能转换为android.widget.RelativeLayout $的LayoutParams
                在com.example.dexter.contentpro.MainActivity.onCreate(MainActivity.java:37)
                在android.app.Activity.performCreate(Activity.java:5933)
                在android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
    在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
    在android.app.ActivityThread.access $ 800(ActivityThread.java:144)
    在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)
    在android.os.Handler.dispatchMessage(Handler.java:102)
    在android.os.Looper.loop(Looper.java:135)
    在android.app.ActivityThread.main(ActivityThread.java:5221)
    在java.lang.reflect.Method.invoke(本机方法)
    在java.lang.reflect.Method.invoke(Method.java:372)
    在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)
    在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

activity_main.xml中 -

 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    的xmlns:工具=htt​​p://schemas.android.com/tool​​s
    机器人:ID =@ + ID / content_layout
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
    机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
    机器人:paddingTop =@扪/ activity_vertical_margin
    机器人:paddingBottom会=@扪/ activity_vertical_margin工具:上下文=MainActivity。&GT;    &LT;的TextView
        机器人:ID =@ + ID / DISPLAY_TEXT
        机器人:layout_width =WRAP_CONTENT
        机器人:layout_height =WRAP_CONTENT/&GT;&LT; / RelativeLayout的&GT;


解决方案

该错误是正确的,你也是对的,但你和Android的一部分,它是一个misinter pretation。你看,当你膨胀 A 查看 Parent_View 是任何子类的的ViewGroup ,Android的回报是查看键,把它像一个查看如果你想要把它像一个子类的ViewGroup 你将有一些投Excetpions ...的您看到我知道这经验,但我不能给你这个证明的事实

回到你的问题;你尝试获得的LayoutParams 你的 RelativeLayout的这是最上面的查看将XML的,所以我想 getLayoutParams()应该返回,因为它没有连接到一个的ViewGroup ,但它不是,这意味着它连接到的FrameLayout 这是最上面的查看窗口或本身。

为了解决这个问题

试试这个的(1)

而不是使用此行

  RelativeLayout.LayoutParams PARAMS =(android.widget.RelativeLayout.LayoutParams)
                          contentLayout.getLayoutParams();

使用

  //获取TextView的孩子中的RelativeLayout中的LayoutParams
//你的XML,这将自动映射到RelativeLayout的PARAMS
RelativeLayout.LayoutParams PARAMS =(android.widget.RelativeLayout.LayoutParams)
                          displayTextView.getLayoutParams();

或使用本的(2)

改变只有你的XML,你的previous code将正常工作

 &LT;的RelativeLayout的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:工具=htt​​p://schemas.android.com/tool​​s
机器人:layout_width =match_parent
机器人:layout_height =match_parent
机器人:paddingLeft =@扪/ activity_horizo​​ntal_margin
机器人:paddingRight =@扪/ activity_horizo​​ntal_margin
机器人:paddingTop =@扪/ activity_vertical_margin
机器人:paddingBottom会=@扪/ activity_vertical_margin
工具:上下文=MainActivity。&GT;   &LT; RelativeLayout的
      机器人:ID =@ + ID / content_layout
      机器人:layout_width =match_parent
      机器人:layout_height =match_parent&GT;        &LT;的TextView
           机器人:ID =@ + ID / DISPLAY_TEXT
           机器人:layout_width =WRAP_CONTENT
           机器人:layout_height =WRAP_CONTENT/&GT;    &LT; / RelativeLayout的&GT;
&LT; / RelativeLayout的&GT;

Here I need to add textview at runtime. first I took reference of RelativeLayout to get layout params but unfortunately its throwing ClassCastException at RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()

I also checked - FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used, but it didn't work. Since after replacing RelativeLayout with FrameLayout, I'm unable to add rule on layoutParams.

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

            TextView displayTextView = (TextView)findViewById(R.id.display_text);

            ContentResolver resolver = getContentResolver();
            Cursor cursor = resolver.query(UserDictionary.Words.CONTENT_URI, null, null, null, null);

            cursor.moveToFirst();
            displayTextView.setText("The User Dictionary contains " + cursor.getColumnCount() + " words");

            RelativeLayout contentLayout = (RelativeLayout)findViewById(R.id.content_layout);

            TextView columnNames = new TextView(this);
            columnNames.setText("Columns: _id - frequency - word");
            contentLayout.addView(columnNames);

            RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams();
            params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
            params.addRule(RelativeLayout.BELOW);

            do{
                TextView entry = new TextView(this);
                entry.setText(cursor.getInt(cursor.getColumnIndex(UserDictionary.Words._ID)) + " - " +
                              cursor.getInt(cursor.getColumnIndex(UserDictionary.Words.FREQUENCY)) + " - " +
                              cursor.getString(cursor.getColumnIndex(UserDictionary.Words.WORD)));

                entry.setLayoutParams(params);
                contentLayout.addView(entry);
            }while(cursor.moveToNext());

            cursor.close();
    }

    **Getting following error** - 
    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.dexter.contentpro/com.example.dexter.contentpro.MainActivity}: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
         Caused by: java.lang.ClassCastException: android.widget.FrameLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
                at com.example.dexter.contentpro.MainActivity.onCreate(MainActivity.java:37)
                at android.app.Activity.performCreate(Activity.java:5933)
                at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
                at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)
                at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
                at android.app.ActivityThread.access$800(ActivityThread.java:144)
                at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
                at android.os.Handler.dispatchMessage(Handler.java:102)
                at android.os.Looper.loop(Looper.java:135)
                at android.app.ActivityThread.main(ActivityThread.java:5221)
                at java.lang.reflect.Method.invoke(Native Method)
                at java.lang.reflect.Method.invoke(Method.java:372)
                at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
                at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)

activity_main.xml-

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/content_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity">

    <TextView
        android:id="@+id/display_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

</RelativeLayout>

解决方案

The Error is right and you are also right,but its a misinterpretation on both you and android's part. You see when you Inflate a View with Parent_View being any sub class of a ViewGroup, android returns that View and treat it like a View if you want to treat it like a subclass of a ViewGroup you are going to have some cast Excetpions... You see i know this by experience but i cant give you a documented fact about this

Back to your question; you try to get the Layoutparams of your RelativeLayout which is the top most View of that xml, so i am thinking getLayoutParams() should return null since its not attached to a ViewGroup but its not which means its attached to a FrameLayout which is the Top most View for the Window or itself.

To solve it

try this (1)

instead of using this line

RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)
                          contentLayout.getLayoutParams();

use this

//get the layoutParams of the TextView child in the Relativelayout in 
//your xml,this will automatically map to the RelativeLayout params
RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)
                          displayTextView.getLayoutParams();    

or use this(2)

change only your xml, and your previous code will work fine

<RelativeLayout 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"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin" 
tools:context=".MainActivity">

   <RelativeLayout
      android:id="@+id/content_layout"
      android:layout_width="match_parent"
      android:layout_height="match_parent" >

        <TextView
           android:id="@+id/display_text"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content" />

    </RelativeLayout>
</RelativeLayout>

这篇关于入门java.lang.ClassCastException:android.widget.FrameLayout $的LayoutParams不能转换为android.widget.RelativeLayout $的LayoutParams的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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