baseAdapter の getView メソッドで、position に従って 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)