我试图根据 position 在 BaseAdapter 的 getView 方法中返回 2 种不同类型的视图。
但我得到了 ClassCastException。下面是我的代码:
public View getView(final int position, View convertView, ViewGroup parent) {
if(position != 5){
WebPageView web;
if(convertView == null){
web = new WebPageView(mContext, new Point(parent.getWidth(), parent.getHeight()), parent);
} else {
web = (WebPageView) convertView;
}
web.setPage("http://yahoo.com");
return web;
}else{
final PageView pageView;
if (convertView == null) {
pageView = new PageView(mContext);
} else {
pageView = (PageView) convertView;
}
return pageView;
}
}
但我总是得到 ClassCastException。当我在 getView 中单独使用 WebPageView 或 PageView 时,我没有收到异常。
任何人有任何想法?
下面是我的 logcat 输出。
10-18 18:03:59.250: E/AndroidRuntime(31390): FATAL EXCEPTION: main
10-18 18:03:59.250: E/AndroidRuntime(31390): java.lang.ClassCastException: com.getit.WebPageView cannot be cast to com.getit.PageView
10-18 18:03:59.250: E/AndroidRuntime(31390): at com.getit.PageAdapter.getView(PageAdapter.java:94)
10-18 18:03:59.250: E/AndroidRuntime(31390): at com.getit.CustomGalleryView.getOrCreateChild(CustomGalleryView.java:467)
10-18 18:03:59.250: E/AndroidRuntime(31390): at com.getit.CustomGalleryView.onLayout(CustomGalleryView.java:422)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.View.layout(View.java:13754)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewGroup.layout(ViewGroup.java:4364)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.RelativeLayout.onLayout(RelativeLayout.java:948)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.View.layout(View.java:13754)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewGroup.layout(ViewGroup.java:4364)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.View.layout(View.java:13754)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewGroup.layout(ViewGroup.java:4364)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1649)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1507)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.LinearLayout.onLayout(LinearLayout.java:1420)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.View.layout(View.java:13754)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewGroup.layout(ViewGroup.java:4364)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.widget.FrameLayout.onLayout(FrameLayout.java:448)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.View.layout(View.java:13754)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewGroup.layout(ViewGroup.java:4364)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1868)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1689)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:1000)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:4214)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.Choreographer$CallbackRecord.run(Choreographer.java:725)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.Choreographer.doCallbacks(Choreographer.java:555)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.Choreographer.doFrame(Choreographer.java:525)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:711)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.os.Handler.handleCallback(Handler.java:615)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.os.Handler.dispatchMessage(Handler.java:92)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.os.Looper.loop(Looper.java:137)
10-18 18:03:59.250: E/AndroidRuntime(31390): at android.app.ActivityThread.main(ActivityThread.java:4745)
10-18 18:03:59.250: E/AndroidRuntime(31390): at java.lang.reflect.Method.invokeNative(Native Method)
10-18 18:03:59.250: E/AndroidRuntime(31390): at java.lang.reflect.Method.invoke(Method.java:511)
10-18 18:03:59.250: E/AndroidRuntime(31390): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
10-18 18:03:59.250: E/AndroidRuntime(31390): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
10-18 18:03:59.250: E/AndroidRuntime(31390): at dalvik.system.NativeStart.main(Native Method)