android - 获取 java.lang.ClassCastException : android. widget.FrameLayout$LayoutParams 无法转换为 android.widget.RelativeLayout$LayoutParams

标签 android android-layout android-contentprovider classcastexception android-relativelayout

这里我需要在运行时添加textview。首先我引用了 RelativeLayout 来获取布局参数,但不幸的是它在 RelativeLayout.LayoutParams params = (android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()

抛出 ClassCastException

我还检查了 - FrameLayout to RelativeLayout ClassCastException even if there is no FrameLayout used ,但它没有用。因为用 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-

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

最佳答案

错误是对的,你也是对的,但它是你和 android 双方的误解。你会看到,当你Inflate View 时,Parent_ViewViewGroup 的任何子类,android 返回那个 View 并将其视为 View 如果您想将其视为 ViewGroup 的子类,您将拥有一些 cast Excetpions... < em>你看,我凭经验知道这一点,但我不能给你关于这方面的有据可查的事实

回到你的问题;您尝试获取 RelativeLayoutLayoutparams,这是该 xml 的最顶层 View,所以我在想 getLayoutParams() 应该返回 null 因为它没有附加到 ViewGroup 但它不是这意味着它附加到 FrameLayout 这是最顶层的ViewWindow 或其本身。

解决

试试这个 (1)

而不是使用这条线

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

用这个

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

或者使用这个(2)

只改变你的 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>

关于android - 获取 java.lang.ClassCastException : android. widget.FrameLayout$LayoutParams 无法转换为 android.widget.RelativeLayout$LayoutParams,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30276389/

相关文章:

android - ConstraintLayout 编辑器添加了不必要的约束或边距,使其无法使用

android - Google Drive 的内容提供者

java - 我可以在 UI 线程之外使用 getFilesDir

android - 折叠工具栏锚定 View 在折叠时隐藏

Android数据绑定(bind)layout_width : You must supply a layout_width attribut

android - 如何在 android 列表适配器中引用 fragment

android - 为什么在我发送 SMS 时我的 ContentObserver 没有被调用?

android - ContentUri 的 id 格式为字符串

java - 如何在 Android 中从/向 Dropbox 导出导入 SQLite 数据库?

android - SharedPreferences 有时会被删除