在 listView 的每 12 个项目之后添加横幅时,如果广告横幅已修复但当我包含浮动横幅时,listView 工作正常。
日志猫:
07-01 10:36:00.352: E/AndroidRuntime(31771): FATAL EXCEPTION: main
07-01 10:36:00.352: E/AndroidRuntime(31771): java.lang.NullPointerException
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.widget.FrameLayout.onLayout(FrameLayout.java:383)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.view.View.layout(View.java:11315)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.view.ViewGroup.layout(ViewGroup.java:4314)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.view.ViewRootImpl.performTraversals(ViewRootImpl.java:1625)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.view.ViewRootImpl.handleMessage(ViewRootImpl.java:2642)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.os.Looper.loop(Looper.java:154)
07-01 10:36:00.352: E/AndroidRuntime(31771): at android.app.ActivityThread.main(ActivityThread.java:4624)
07-01 10:36:00.352: E/AndroidRuntime(31771): at java.lang.reflect.Method.invokeNative(Native Method)
07-01 10:36:00.352: E/AndroidRuntime(31771): at java.lang.reflect.Method.invoke(Method.java:511)
07-01 10:36:00.352: E/AndroidRuntime(31771): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
07-01 10:36:00.352: E/AndroidRuntime(31771): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
07-01 10:36:00.352: E/AndroidRuntime(31771): at dalvik.system.NativeStart.main(Native Method)
类适配器:
public MyAdapter(Activity context, ImageManager imageManager) {
this.context = context;
annoncesList.clear();
bannersPositionSet.clear();
}
@Override
public int getCount()
{
return annoncesList.size();
}
@Override
public Object getItem(int position)
{
int div = position / 12;
int restDiv = position % 12;
if (restDiv == div - 1)
return (String) annoncesList.get(position);
else
return (Annonce) annoncesList.get(position);
}
@Override
public long getItemId(int position)
{
return position + 1;
}
@Override
public int getItemViewType(int position)
{
if (bannersPositionSet.contains(position))
{
return TYPE_BANNER;
}
else
return TYPE_ANNONCE;
}
@Override
public int getViewTypeCount()
{
return TYPE_COUNT; // Count of different layouts
}
public void addBannerItem()
{
annoncesList.add("banner");
notifyDataSetChanged();
// save banner position
bannersPositionSet.add(annoncesList.size() - 1);
}
public void addAllAnnonceItems(List<Annonce> annonceList)
{
annoncesList.addAll(annonceList);
notifyDataSetChanged();
}
public void clearList()
{
annoncesList.clear();
notifyDataSetChanged();
}
/**
* Destroy all banners to free memory
*
* this method is used in the method: onDestroy of activity annonceList
*/
public void destroyBanner()
{
for (SASBannerView banner : bannerList)
{
banner.onDestroy();
}
}
@Override
public View getView(final int position, View convertView, ViewGroup parent)
{
ViewHolder viewHolder;
int type = getItemViewType(position);
if (convertView == null)
{
viewHolder = new ViewHolder();
switch (type)
{
case TYPE_ANNONCE:
convertView = inflater.inflate(R.layout.annonce_row, null);
viewHolder.annonceImage = (ImageView) convertView.findViewById(R.id.annonce_image);
viewHolder.annonceTitle = (TextView) convertView.findViewById(R.id.annonce_title);
viewHolder.annonceAddress = (TextView) convertView.findViewById(R.id.annonce_seller_city);
viewHolder.annoncePrice = (TextView) convertView.findViewById(R.id.annonce_price);
viewHolder.annonceEuroSymbole = (TextView) convertView.findViewById(R.id.euro_symbole);
viewHolder.annonceFlag = (ImageView) convertView.findViewById(R.id.annonce_flag);
viewHolder.annonceDate = (TextView) convertView.findViewById(R.id.annonce_date);
viewHolder.annonceTitle.setTypeface(tfLatoBol);
viewHolder.annonceAddress.setTypeface(tfLatoReg);
viewHolder.annoncePrice.setTypeface(tfLatoBol);
viewHolder.annonceEuroSymbole.setTypeface(tfLatoBol);
viewHolder.annonceDate.setTypeface(tfLatoReg);
break;
case TYPE_BANNER:
convertView = inflater.inflate(R.layout.floating_android_banner, null);
viewHolder.floatingBannerView = (SASBannerView) convertView.findViewById(R.id.floating_banner);
final SASBannerView sasBannerView = viewHolder.floatingBannerView;
floatingBannerResponseHandler = new SASAdView.AdResponseHandler()
{
public void adLoadingCompleted(SASAdElement adElement)
{
DebugLog.i(TAG, "floating Banner loading completed");
context.runOnUiThread(new Runnable()
{
@Override
public void run()
{
sasBannerView.setVisibility(View.VISIBLE);
}
});
}
public void adLoadingFailed(Exception e)
{
context.runOnUiThread(new Runnable()
{
@Override
public void run()
{
sasBannerView.setVisibility(View.GONE);
}
});
DebugLog.i(TAG, "floating Banner loading failed: " + e.getMessage());
}
};
break;
default:
break;
}
convertView.setTag(viewHolder);
}
else
{
viewHolder = (ViewHolder) convertView.getTag();
}
if (type == TYPE_BANNER)
{
loadFloatingBannerView(viewHolder.floatingBannerView, floatingBannerResponseHandler);
bannerList.add(viewHolder.floatingBannerView);
}
else if (type == TYPE_ANNONCE)
{
Annonce annonce = (Annonce) this.getItem(position);
viewHolder.annonceTitle.setText(Html.fromHtml(annonce.getTitle()));
}
return convertView;
}
/**
*
* Load floating banner view
*
*/
private void loadFloatingBannerView(SASBannerView floatingBanner,
SASAdView.AdResponseHandler floatingBannerResponseHandler)
{
// load floating banner ad with appropriate parameters
// (siteID,pageID,formatID,master,targeting,adResponseHandler)
floatingBanner.loadAd(Constants.BANNER_BETWEEN_ANNONCES_SITE_ID, Constants.BANNER_BETWEEN_ANNONCES_PAGE_ID,
Constants.BANNER_BETWEEN_ANNONCES_FORMAT_ID, true, Constants.BANNER_BETWEEN_ANNONCES_TARGET,
floatingBannerResponseHandler);
}
private static class ViewHolder
{
public ImageView annonceImage;
public TextView annonceTitle;
public TextView annonceAddress;
public TextView annoncePrice;
public TextView annonceEuroSymbole;
public ImageView annonceFlag;
public TextView annonceDate;
public SASBannerView floatingBannerView;
}
}