1

我试图根据 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)
4

1 回答 1

4

您会收到此错误,因为您尝试回收已被转换为另一个对象的视图。我不知道您是否使用自定义对象,就好像两者都在扩展一个可以工作的公共类一样。您可以做两件事:

1:检查转换视图是否是您的自定义对象的一个​​实例。这可确保您不会尝试将网页浏览量转换为网页浏览量,反之亦然

if (convertView == null || !convertView instanceof PageView) {
    pageView = new PageView(mContext);
} else {
    pageView = (PageView) convertView;
}

同样检查webview。

2:每次实例化一个新对象

if(position != 5){
WebPageView web;
web = new WebPageView(mContext, new Point(parent.getWidth(), parent.getHeight()), parent);
web.setPage("http://yahoo.com");
return web;
}else{
final PageView pageView;
pageView = new PageView(mContext);
return pageView;
}

不是很建议,但仍然...

于 2012-10-18T13:39:55.610 回答