java.lang.ClassCastException:android.view.ViewGroup $ LayoutParams无法强制转换为android.widget.RelativeLayout $ LayoutParams

时间:2013-10-26 09:07:46

标签: java android xml relative

您好我刚刚编辑了我的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>

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

你可以帮助我,如何解决它?

4 个答案:

答案 0 :(得分:20)

在您的代码中,您要导入import android.widget.RelativeLayout.LayoutParams;而不是android.view.ViewGroup.LayoutParams

所以删除import android.widget.RelativeLayout.LayoutParams;

并添加import android.view.ViewGroup.LayoutParams

答案 1 :(得分:2)

您应该创建父布局的LayoutParams对象。在下面的布局中设置LinearLayout的高度和宽度的示例,您必须使RelativeLayout的LayoutParmas本身不是LinearLayout。

$access_logs = scandir($log_list);

$month = date("M-Y");
$prevmonth = date('M-Y', strtotime("first day of last month"));
$minus_2_month = date('M-Y', strtotime("first day of -2 month"));
$minus_3_month = date('M-Y', strtotime("first day of -3 month"));
$minus_4_month = date('M-Y', strtotime("first day of -4 month"));
//etc etc etc

    foreach($access_logs as $access_log) {

            if ($access_log == $domain_dashes . '.mydomain.com-' . $month . '.gz') {
                $log_month = '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n            ";
            }

            if ($access_log == $domain_dashes . '.mydomain.com-' . $prevmonth . '.gz') {
                $log_month .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n            ";
            }

            if ($access_log == $domain_dashes . '.mydomain.com-' . $minus_2_month . '.gz') {
                $log_month .= '<li><a href="logs.php?log=' . $access_log . '">' . $access_log . '</a></li>' . "\n            ";
            }

           // etc etc etc
    }

答案 2 :(得分:0)

只需点击 Cnt + o 即可删除不需要的导入

答案 3 :(得分:0)

使用以下代码:

imageView.setLayoutParams(
  new android.widget.RelativeLayout.LayoutParams(
    ViewPager.LayoutParams.MATCH_PARENT,
    ViewPager.LayoutParams.MATCH_PARENT));

如果您遇到任何错误,请告诉我。