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

在這里,我需要在運行時添加textview。 首先我參考了RelativeLayout來獲取布局參數但不幸的是它在RelativeLayout.LayoutParams params =(android.widget.RelativeLayout.LayoutParams)contentLayout.getLayoutParams()中拋出ClassCastException

我還檢查了 - FrameLayout到RelativeLayout ClassCastException,即使沒有使用FrameLayout ,但它不起作用。 由於在用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的部分都有誤解。 你看到當你使用Parent_View作為ViewGroup任何子類來Inflate一個View ,如果你想將它視為一個ViewGroup的子類,那么你會看到View並將其視為一個View ,你將會有一些轉換Excetpions ...... 你看我通過經驗知道這一點,但我不能給你一個記錄在案的事實

回到你的問題; 你試圖得到你的RelativeLayoutLayoutparams ,這是該xml的最頂級的View ,所以我認為getLayoutParams()應該返回null因為它沒有附加到ViewGroup但它不是意味着它附加到一個FrameLayout是頂部大多數Window或其自身的View

解決它

試試這個 (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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM