我想知道为什么在尝试运行程序时通过代码动态更改嵌套在RelativeLayout内的LinearLayout宽度和高度时抛出ClassCastException的原因。

更重要的是,我该如何解决?我是java和android的新手,请尽可能清楚地声明。谢谢任何人的帮助!!!

Java文件(MainActivity.java)

import android.app.Activity;
import android.os.Bundle;
import android.view.ViewGroup;
import android.widget.LinearLayout;

public class MainActivity extends Activity {

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

        //To search for the LinearLayout
        LinearLayout ll = (LinearLayout) findViewById(R.id.ll);
        //To set new height and width of Linear Layout which is nested inside the Relative Layout
       //Even changing the class name "View Group" below to "Linear Layout" throw Class Cast Exception as well
        ll.setLayoutParams(new ViewGroup.LayoutParams(100,200));

}
}

Xml文件(activity_main.xml)
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools"
        android:id="@+id/RelativeLayout1"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
<!-- The width and height here is going to be changed by code while the program run -->
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:background="#FF0000"
            android:orientation="vertical"
            android:id="@+id/ll">
        </LinearLayout>

    </RelativeLayout>

抛出异常
03-31 12:55:21.862: D/AndroidRuntime(734): Shutting down VM
03-31 12:55:21.862: W/dalvikvm(734): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
03-31 12:55:21.872: E/AndroidRuntime(734): FATAL EXCEPTION: main
03-31 12:55:21.872: E/AndroidRuntime(734): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1317)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1264)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:292)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:314)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.View.measure(View.java:12723)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.View.measure(View.java:12723)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1369)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.LinearLayout.measureVertical(LinearLayout.java:660)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.LinearLayout.onMeasure(LinearLayout.java:553)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.View.measure(View.java:12723)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4698)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.widget.FrameLayout.onMeasure(FrameLayout.java:293)
03-31 12:55:21.872: E/AndroidRuntime(734):  at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2092)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.View.measure(View.java:12723)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1064)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2442)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.os.Handler.dispatchMessage(Handler.java:99)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.os.Looper.loop(Looper.java:137)
03-31 12:55:21.872: E/AndroidRuntime(734):  at android.app.ActivityThread.main(ActivityThread.java:4424)
03-31 12:55:21.872: E/AndroidRuntime(734):  at java.lang.reflect.Method.invokeNative(Native Method)
03-31 12:55:21.872: E/AndroidRuntime(734):  at java.lang.reflect.Method.invoke(Method.java:511)
03-31 12:55:21.872: E/AndroidRuntime(734):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
03-31 12:55:21.872: E/AndroidRuntime(734):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
03-31 12:55:21.872: E/AndroidRuntime(734):  at dalvik.system.NativeStart.main(Native Method)
03-31 12:55:22.152: I/dalvikvm(734): threadid=3: reacting to signal 3
03-31 12:55:22.162: I/dalvikvm(734): Wrote stack traces to '/data/anr/traces.txt'
03-31 12:55:22.392: I/dalvikvm(734): threadid=3: reacting to signal 3
03-31 12:55:22.402: I/dalvikvm(734): Wrote stack traces to '/data/anr/traces.txt'
03-31 12:55:32.152: I/Process(734): Sending signal. PID: 734 SIG: 9

最佳答案

您必须使用的LayoutParams的类型由封装要更改其参数的事物的Layout的类型指定。

因此,例如,如果您想更改LayoutParamsLinearLayout,则需要检查封装它的Layout类型是什么。在您的情况下,LinearLayoutRelativeLayout封装,因此您需要使用RelativeLayout.LayoutParams而不是ViewGroup.LayoutParams

关于java - 将LinearLayout嵌套到RelativeLayout中时,设置LinearLayout的LayoutParams,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/29369946/

10-08 23:25