Android ListView ClassCastException

时间:2015-03-18 11:02:51

标签: android listview exception ads

我想在ListView中集成广告,我有以下代码:

更新了AdViewHelper.java

  public LinearLayout configureAdViewWithLayoutContainer(final Activity context) {

    final LinearLayout layout = new LinearLayout(context);
    layout.setLayoutParams(new AbsListView.LayoutParams(AbsListView.LayoutParams.MATCH_PARENT,
            AbsListView.LayoutParams.WRAP_CONTENT));
    layout.setGravity(Gravity.CENTER);

    PublisherAdView adView = new PublisherAdView(context);
    adView.setLayoutParams(new LinearLayout.LayoutParams(context.getResources().getInteger(R.integer.google_ads_width),
            context.getResources().getInteger(R.integer.google_ads_height)));
    adView.setId(R.id.ad_view_id);
    adView.setAdUnitId("MY_UNIT_ID");
    adView.setAdSizes(getAdSize(context));
    adView.setVisibility(View.GONE);
    final PublisherAdView finalAdView = adView;
    adView.setAdListener(new AdListener() {
        @Override
        public void onAdLoaded() {
            super.onAdLoaded();
            finalAdView.setVisibility(View.VISIBLE);
        }

        @Override
        public void onAdFailedToLoad(int errorCode) {
            super.onAdFailedToLoad(errorCode);
        }
    });
    adView.setAppEventListener(this);

    layout.addView(adView);

    return layout;
}

MyAdapter.java

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
if (getItemViewType(position) == AD_TYPE) {
     return adViewHelper.configureAdViewWithLayoutContainer(activity);    
 } else {
      return convertView = inflater.inflate(R.layout.product_row, parent, false);
    }
}

注意:请忽略ViewHolder并回收。这不是这个问题的目的。

所以,问题是我收到以下错误:

java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.AbsListView$LayoutParams
        at android.widget.ListView.setupChild(ListView.java:1826)
        at android.widget.ListView.makeAndAddView(ListView.java:1793)
        at android.widget.ListView.fillDown(ListView.java:691)
        at android.widget.ListView.fillFromTop(ListView.java:752)
        at android.widget.ListView.layoutChildren(ListView.java:1630)
        at android.widget.AbsListView.onLayout(AbsListView.java:2087)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.RelativeLayout.onLayout(RelativeLayout.java:1055)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1671)
        at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1525)
        at android.widget.LinearLayout.onLayout(LinearLayout.java:1434)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.widget.FrameLayout.layoutChildren(FrameLayout.java:453)
        at android.widget.FrameLayout.onLayout(FrameLayout.java:388)
        at android.view.View.layout(View.java:14817)
        at android.view.ViewGroup.layout(ViewGroup.java:4631)
        at android.view.ViewRootImpl.performLayout(ViewRootImpl.java:1983)
        at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1740)
        at android.view.ViewRootImpl.doTraversal(ViewRootImpl.java:996)
        at android.view.ViewRootImpl$TraversalRunnable.run(ViewRootImpl.java:5600)
        at android.view.Choreographer$CallbackRecord.run(Choreographer.java:761)
        at android.view.Choreographer.doCallbacks(Choreographer.java:574)
        at android.view.Choreographer.doFrame(Choreographer.java:544)
        at android.view.Choreographer$FrameDisplayEventReceiver.run(Choreographer.java:747)
        at android.os.Handler.handleCallback(Handler.java:733)
        at android.os.Handler.dispatchMessage(Handler.java:95)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5001)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
        at dalvik.system.NativeStart.main(Native Method)

我知道这是因为我的PublisherAdView位于LinearLayout中,但我需要使用LinearLayout才能使广告的可见性发挥作用(当用户打开屏幕时,广告行应该被隐藏,直到加载为止)。

我怎样才能使用LinearLayout然后摆脱这个类强制转换异常?

由于

1 个答案:

答案 0 :(得分:1)

它发生在这里:

layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
            LinearLayout.LayoutParams.WRAP_CONTENT));
ListView中使用了

layout,所以它应该有AbsListView.LayoutParams

然后在这里:

adView.setLayoutParams(new AbsListView.LayoutParams(
        context.getResources().getInteger(R.integer.google_ads_width),
        context.getResources().getInteger(R.integer.google_ads_height)));

您的layout是一个LinearLayout,所以它的子节点应该有LinearLayout.LayoutParams。

所以你现在基本上反过来了:)