我的初始代码行位于被覆盖的方法内onGlobalLayout()
:
img.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, 100));
覆盖方法中发生的 ClassCastException onGlobalLayout()
-
错误日志:
09-02 14:25:35.326: E/AndroidRuntime(5187): java.lang.ClassCastException: android.view.ViewGroup$LayoutParams cannot be cast to android.support.v4.view.ViewPager$LayoutParams
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1319)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.view.View.measure(View.java:15181)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.widget.RelativeLayout.measureChildHorizontal(RelativeLayout.java:617)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.widget.RelativeLayout.onMeasure(RelativeLayout.java:399)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.view.View.measure(View.java:15181)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:4814)
09-02 14:25:35.326: E/AndroidRuntime(5187): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1390)
当我执行以下操作以解决其他 SO 帖子中指出的上述错误时:
img.setLayoutParams(new ViewPager.LayoutParams(ViewPager.LayoutParams.MATCH_PARENT, 100));
然后,我得到编译时错误:
The constructor ViewPager.LayoutParams(int, int) is undefined
xml 中的 ViewPager 定义如下:
<android.support.v4.view.ViewPager
android:id="@+id/HView"
android:layout_width="560dp"
android:layout_height="255dp"
android:layout_centerHorizontal="true"
android:layout_marginLeft="160sp"
android:layout_marginTop="110sp"
android:layout_marginBottom="80sp">
</android.support.v4.view.ViewPager>