我遇到了这个奇怪的问题,我无法在ViewPager中使用FrameLayout。当它是RelativeLayout时,它一切都很好并且安全。现在,当我想要更改为FrameLayout时 - 它在开始时崩溃:
12-17 18:03:30.528: E/AndroidRuntime(19357): FATAL EXCEPTION: main
12-17 18:03:30.528: E/AndroidRuntime(19357): Process: pl.dajsiezlapac.apka, PID: 19357
12-17 18:03:30.528: E/AndroidRuntime(19357): java.lang.ClassCastException: android.widget.FrameLayout cannot be cast to android.widget.RelativeLayout
12-17 18:03:30.528: E/AndroidRuntime(19357): at pl.dajsiezlapac.apka.ViewPagerAdapter.isViewFromObject(ViewPagerAdapter.java:52)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.support.v4.view.ViewPager.infoForChild(ViewPager.java:1333)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.support.v4.view.ViewPager.populate(ViewPager.java:1084)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.support.v4.view.ViewPager.populate(ViewPager.java:919)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.support.v4.view.ViewPager.onMeasure(ViewPager.java:1441)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.View.measure(View.java:16540)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.widget.LinearLayout.measureChildBeforeLayout(LinearLayout.java:1404)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.widget.LinearLayout.measureVertical(LinearLayout.java:695)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.widget.LinearLayout.onMeasure(LinearLayout.java:588)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.View.measure(View.java:16540)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.View.measure(View.java:16540)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-17 18:03:30.528: E/AndroidRuntime(19357): at com.android.internal.widget.ActionBarOverlayLayout.onMeasure(ActionBarOverlayLayout.java:327)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.View.measure(View.java:16540)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewGroup.measureChildWithMargins(ViewGroup.java:5137)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.widget.FrameLayout.onMeasure(FrameLayout.java:310)
12-17 18:03:30.528: E/AndroidRuntime(19357): at com.android.internal.policy.impl.PhoneWindow$DecorView.onMeasure(PhoneWindow.java:2291)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.View.measure(View.java:16540)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewRootImpl.performMeasure(ViewRootImpl.java:1942)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewRootImpl.measureHierarchy(ViewRootImpl.java:1132)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1321)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1019)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5725)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.Choreographer.doCallbacks(Choreographer.java:574)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.Choreographer.doFrame(Choreographer.java:544)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.os.Handler.handleCallback(Handler.java:733)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.os.Handler.dispatchMessage(Handler.java:95)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.os.Looper.loop(Looper.java:136)
12-17 18:03:30.528: E/AndroidRuntime(19357): at android.app.ActivityThread.main(ActivityThread.java:5086)
12-17 18:03:30.528: E/AndroidRuntime(19357): at java.lang.reflect.Method.invokeNative(Native Method)
12-17 18:03:30.528: E/AndroidRuntime(19357): at java.lang.reflect.Method.invoke(Method.java:515)
12-17 18:03:30.528: E/AndroidRuntime(19357): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
12-17 18:03:30.528: E/AndroidRuntime(19357): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
12-17 18:03:30.528: E/AndroidRuntime(19357): at dalvik.system.NativeStart.main(Native Method)
这是ViewPagerAdapter.java(分别为50,51和52行 - 所以问题出在' return'):
@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
答案 0 :(得分:1)
FrameLayout
和RelativeLayout
是不同的类。您无法将FrameLayout
转换为RelativeLayout
。
摆脱isViewFromObject()
中的演员表。或者,至少将其投放到View
或FrameLayout
(这将是有效的)而不是RelativeLayout
(这是无效的)。