web-dev-qa-db-ja.com

java.lang.ClassCastException:Android.view.ViewGroup $ LayoutParamsをAndroid.widget.RelativeLayout $ LayoutParamsにキャストできません

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)
9
bukanamay

コードでは、import Android.widget.RelativeLayout.LayoutParams;ではなくAndroid.view.ViewGroup.LayoutParamsをインポートしています。

したがって、import Android.widget.RelativeLayout.LayoutParams;を削除します

import Android.view.ViewGroup.LayoutParamsを追加します

20
kalyan pvs

親レイアウトのLayoutParamsオブジェクトを作成する必要があります。以下のレイアウトの例で、LinearLayoutの高さと幅を設定するには、LinearLayout自体ではなくRelativeLayoutのLayoutParmasを作成する必要があります。

  <?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
3
Balman Rawat

クリックするだけ Cnt+o 不要なインポートを削除する

0
praveen s

以下のコードを使用してください。

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

エラーが発生した場合はお知らせください。

0
Zafer