ClassCastException:android.widget.LinearLayout $ LayoutParams无法强制转换为android.support.v4.view.ViewPager $ LayoutParams

时间:2014-12-04 08:25:01

标签: android android-layout android-viewpager

我的文件包含与ViewPager相关的以下代码 -

   viewPager = (ViewPager) view.findViewById(R.id.HView);
   adapter = new MyPagerAdapter(getActivity());
   viewPager.setAdapter(adapter);

   linearLayout.getChildAt(0).setLayoutParams(new   
   LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
   (int) (imgHeight * 1.5)));

   linearLayout.setLayoutParams(new  
   LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, 
   (int) (imgHeight * 1.5)));

其中,

View android.view.ViewGroup.getChildAt(int index)

public View getChildAt (int index) 
Added in API level 1
Returns the view at the specified position in the group.

我的适配器在同一个类中 -

public class MyPagerAdapter extends PagerAdapter {

        public MyPagerAdapter(Context context) {
        }

        @Override
        public void destroyItem(View view, int arg1, Object object) {
            try {
                ((ViewPager) view).removeView((LinearLayout) object);
            } catch (Exception e) {
                e.printStackTrace();
            }
        }

        @Override
        public void finishUpdate(View arg0) {

        }

        @Override
        public int getCount() {
            return views.size();
        }

        @Override
        public Object instantiateItem(View view, int position) {

            View myView = views.get(position);
            myView.invalidate();

            try
            {
                ((ViewPager) view).removeView(myView);
                ((ViewPager) view).addView(myView);
            }
            catch(IllegalStateException e)
            {
                tap.getEntriesByRule(view);
                e.printStackTrace();
            }
            catch(ClassCastException e)
            {
                tap.getEntriesByRule(view);
                e.printStackTrace();
            }
            return myView;
        }

        @Override
        public boolean isViewFromObject(View view, Object object) {
            return view == object;
        }

        @Override
        public void restoreState(Parcelable arg0, ClassLoader arg1) {

        }

        @Override
        public Parcelable saveState() {
            return null;
        }

        @Override
        public void startUpdate(View arg0) {

        }

        @Override
        public int getItemPosition(Object object){
             return POSITION_NONE;
        }
    }

XML文件 -

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/chartmain"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="#ffffff"
    android:gravity="center_horizontal|center_vertical"
    android:orientation="vertical" >

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical" >

        <RelativeLayout
            android:id="@+id/relative"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_marginLeft="5dp" >


            <android.support.v4.view.ViewPager
                android:id="@+id/HView"
                android:layout_width="560dp"
                android:layout_height="255dp"
                android:layout_centerHorizontal="true"
                android:layout_marginLeft="160dp"
                android:layout_marginTop="110dp"
                android:layout_marginBottom="80dp">


            </android.support.v4.view.ViewPager>

            <TextView
                android:id="@+id/totalHoursBottom"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignLeft="@+id/HView"
                android:layout_marginLeft="550dp"
                android:layout_marginTop="345dp"
                android:textSize="14dp"
                android:text="Total Hours"
                android:textColor="#000000" />

            <TextView
                android:id="@+id/textView3"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignTop="@+id/textView2"
                android:text="Mid Night"
                android:textSize="14dp"
                android:textColor="#000000" />

        </RelativeLayout>

    </LinearLayout>

</LinearLayout>

这是错误日志 -

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams
at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1319)
at android.view.View.measure(View.java:15609)
at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:681)
at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:461)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.widget.LinearLayout.measureHorizontal(LinearLayout.java:1041)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:590)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1411)
at android.widget.LinearLayout.measureVertical(LinearLayout.java:698)
at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
at android.view.View.measure(View.java:15609)
at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4916)
at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2191)
at android.view.View.measure(View.java:15609)
at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:2165)
at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1249)
at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1443)
at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1139)
at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4879)
at android.view.Choreographer$CallbackRecord.run(Choreographer.java:776)
at android.view.Choreographer.doCallbacks(Choreographer.java:579)
at android.view.Choreographer.doFrame(Choreographer.java:548)
at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:762)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:153)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:4)

如果您将View添加到指定ViewGroup的{​​{1}},则LayoutParams必须是该特定LayoutParams的嵌套类。因此,在您的情况下,您必须使用ViewGroup而不是ViewPager#LayoutParams