java.lang.ClassCastException: android.view.ViewGroup$LayoutParams 不能转换为 android.widget.RelativeLayout$LayoutParams
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19604891/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
提问by bukanamay
I've just edited my XML, I've put slide menu code in my XML, but there is an error.
我刚刚编辑了我的 XML,我已经在我的 XML 中放入了幻灯片菜单代码,但是有一个错误。
<?xml version="1.0" encoding="utf-8"?>
<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:background="#ffffff" >
<ListView
android:id="@+id/menu_content_menulist"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="8dp"
android:cacheColorHint="@android:color/transparent"
android:divider="@null"
android:listSelector="@null"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mozaik.tangsel.side_menu_scroll.ScrollerLinearLayout
android:id="@+id/menu_content_side_slide_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="verti_
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/content_statis"
android:orientation="vertical"
android:background="@drawable/bg_app"
>
<include layout="@layout/layout_action_bar" />
<ScrollView
android:id="@+id/SV"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="true"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="4dp"
android:background="@drawable/inbox_interface"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="@+id/ImageMozaikTangsel"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="10dip" />
</LinearLayout>
<LinearLayout
android:id="@+id/tabMozaikTangselLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
</com.mozaik.tangsel.side_menu_scroll.ScrollerLinearLayout>
</RelativeLayout>
</RelativeLayout>
Error in logcat:
logcat 中的错误:
10-26 15:53:32.126: E/AndroidRuntime(10869): FATAL EXCEPTION: main
10-26 15:53:32.126: E/AndroidRuntime(10869): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.RelativeLayout$DependencyGraph.findRoots(RelativeLayout.java:1352)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.RelativeLayout$DependencyGraph.getSortedViews(RelativeLayout.java:1299)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.RelativeLayout.sortChildren(RelativeLayout.java:316)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:337)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.View.measure(View.java:15193)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.View.measure(View.java:15193)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.LinearLayout.measureVertical(LinearLayout.java:681)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.LinearLayout.onMeasure(LinearLayout.java:574)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.View.measure(View.java:15193)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4816)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
10-26 15:53:32.126: E/AndroidRuntime(10869): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2217)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.View.measure(View.java:15193)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1850)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1102)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1275)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4216)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.Choreographer.doFrame(Choreographer.java:525)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.os.Handler.handleCallback(Handler.java:615)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.os.Handler.dispatchMessage(Handler.java:92)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.os.Looper.loop(Looper.java:137)
10-26 15:53:32.126: E/AndroidRuntime(10869): at android.app.ActivityThread.main(ActivityThread.java:4794)
10-26 15:53:32.126: E/AndroidRuntime(10869): at java.lang.reflect.Method.invokeNative(Native Method)
10-26 15:53:32.126: E/AndroidRuntime(10869): at java.lang.reflect.Method.invoke(Method.java:511)
10-26 15:53:32.126: E/AndroidRuntime(10869): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
10-26 15:53:32.126: E/AndroidRuntime(10869): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
10-26 15:53:32.126: E/AndroidRuntime(10869): at dalvik.system.NativeStart.main(Native Method)
采纳答案by kalyan pvs
In your code you are importing import android.widget.RelativeLayout.LayoutParams;
instead of android.view.ViewGroup.LayoutParams
在您的代码中,您正在导入import android.widget.RelativeLayout.LayoutParams;
而不是android.view.ViewGroup.LayoutParams
so Delete import android.widget.RelativeLayout.LayoutParams;
所以删除 import android.widget.RelativeLayout.LayoutParams;
and Add import android.view.ViewGroup.LayoutParams
并添加 import android.view.ViewGroup.LayoutParams
回答by praveen s
just click Cnt+ofor delete unwanted imports
只需单击即可Cnt+o删除不需要的导入
回答by Balman Rawat
You should make the LayoutParams object of the parent layout. Example in the layout below to set height and width of the LinearLayout you have to make the LayoutParmas of RelativeLayout not LinearLayout itself.
您应该创建父布局的 LayoutParams 对象。下面的布局示例设置 LinearLayout 的高度和宽度,您必须使 RelativeLayout 的 LayoutParmas 不是 LinearLayout 本身。
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".newsblog.NewsDetailsActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</RelativeLayout>
Java:
爪哇:
//IN ACTIVITY
RelativeLayout.LayoutParams params=new RelativeLayout.LayoutParams(30,30); //Parent Params
linearLayout.setLayoutParmas(params); //Child Layout
//If you want to set the params of the Relative Layout above. It seems it doesn't have any parent but it has a parent created by android framework which is a frame layout. So
FrameLayout.LayoutParmas params=new FrameLayout.LayoutParmas(30, 30);//FrameLayout is a parentcreated by anroid frame work
relativeLayout.setLayoutParmas(params); ////Here Relative layout is child
回答by Zafer
Use code below:
使用下面的代码:
imageView.setLayoutParams(
new android.widget.RelativeLayout.LayoutParams(
ViewPager.LayoutParams.MATCH_PARENT,
ViewPager.LayoutParams.MATCH_PARENT));
Do let me know if you face any error.
如果您遇到任何错误,请告诉我。