简体   繁体   English

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

Here I need to add textview at runtime. 在这里,我需要在运行时添加textview。 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() 首先我参考了RelativeLayout来获取布局参数但不幸的是它在RelativeLayout.LayoutParams params =(android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()中抛出ClassCastException

I also checked - FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used , but it didn't work. 我还检查了 - FrameLayout到RelativeLayout ClassCastException,即使没有使用FrameLayout ,但它不起作用。 Since after replacing RelativeLayout with FrameLayout, I'm unable to add rule on layoutParams. 由于在用FrameLayout替换RelativeLayout之后,我无法在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- 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. 错误是对的,你也是对的,但它对你和android的部分都有误解。 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 你看到当你使用Parent_View作为ViewGroup任何子类来Inflate一个View ,如果你想将它视为一个ViewGroup的子类,那么你会看到View并将其视为一个View ,你将会有一些转换Excetpions ...... 你看我通过经验知道这一点,但我不能给你一个记录在案的事实

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. 你试图得到你的RelativeLayoutLayoutparams ,这是该xml的最顶级的View ,所以我认为getLayoutParams()应该返回null因为它没有附加到ViewGroup但它不是意味着它附加到一个FrameLayout是顶部大多数Window或其自身的View

To solve it 解决它

try this (1) 试试这个 (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) 或使用此 (2)

change only your xml, and your previous code will work fine 只更改你的xml,你以前的代码将正常工作

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.FrameLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.widget.FrameLayout $ LayoutParams - ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.widget.LinearLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.LinearLayout$LayoutParams java.lang.ClassCastException:android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams - java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams Java.lang.ClassCastException:无法将android.widget.RelativeLayout强制转换为android.widget.FrameLayout - Java.lang.ClassCastException: android.widget.RelativeLayout cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.LinearLayout $ LayoutParams无法转换为android.widget.FrameLayout - java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法强制转换为android.support.v7.widget.RecyclerView $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v7.widget.RecyclerView$LayoutParams java.lang.ClassCastException:android.widget.RelativeLayout $ LayoutParams无法转换为android.support.v4.widget.DrawerLayout $ LayoutParams - java.lang.ClassCastException: android.widget.RelativeLayout$LayoutParams cannot be cast to android.support.v4.widget.DrawerLayout$LayoutParams ClassCastException:android.widget.LinearLayout$LayoutParams 不能转换为 android.widget.FrameLayout$LayoutParams - ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams LinearLayout$LayoutParams 不能转换为 android.widget.FrameLayout$LayoutParams - LinearLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM