简体   繁体   中英

How to add adMob Banner into getView using the latest SDK - com.google.android.gms.ads

I'm trying to add adMob Banner ads into the getView method used for my ListView in one of my fragments. I'm using the latest adMob SDK (com.google.android.gms.ads.).

I'm only finding examples which use the older versions of the Google SDK. If anyone has a link to example of a current version of Google admob please let me know.

FYI - I took Tim's suggestion. I found a complete sample of code from http://jmsliu.com/1482/adding-admob-banner-in-android-listview-header.html.

Its a different one that I first showed you but is set up with 3 Activities. After several hours of testing I thought I would show you my code and the error I'm currently getting, and the original code. I added "Log.i" throughout the program to determine which line causes the abend. It appears that the ads is returning null. Also, note that the original version of code (after the error section) has special "keytwordSet" logic. I was unable to get rid of the syntax errors so I removed it from my version.

AdRequest adRequest = new AdRequest(); Set keywordsSet = new HashSet(); keywordsSet.add("game"); keywordsSet.add("dating"); keywordsSet.add("money"); keywordsSet.add("girl"); adRequest.addKeywords(keywordsSet); adView.loadAd(adRequest);

New Code:

  • PostItemAdapter.java
    • Adapter Class which configs and returns the View for ListView
  • */ package com.jms.admobinlistview;

    import java.util.HashSet; import java.util.Set; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.mediation.admob.AdMobExtras; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.LinearLayout; import android.widget.TextView;

    public class PostItemAdapter extends ArrayAdapter { private LayoutInflater inflater; private PostData[] datas; private Activity mainActivity; private AdView adView;

    public PostItemAdapter(Context context, int textViewResourceId, PostData[] objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub mainActivity = (Activity) context; inflater = mainActivity.getLayoutInflater(); datas = objects; }

    static class ViewHolder { TextView postTitleView; TextView postDateView; ImageView postThumbView; }

    public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder;

      if (position == 0) { if (convertView == null || !(convertView instanceof AdView)) { if (adView == null) { Log.i("1", "msg1"); AdView adView = new AdView(mainActivity); Log.i("2", "msg2"); adView.setAdUnitId("ca-app-pub-4409647944042499/8301167762"); Log.i("3", "msg3"); adView.setAdSize(AdSize.BANNER); Log.i("4", "msg4"); // Convert the default layout parameters so that they play > nice with // ListView. float density = mainActivity.getResources().getDisplayMetrics> ().density; int height = Math.round(AdSize.BANNER.getHeight() * density); AbsListView.LayoutParams params = new AbsListView.LayoutParams( AbsListView.LayoutParams.FILL_PARENT, height); adView.setLayoutParams(params); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("TEST_DEVICE_ID") .build(); Log.i("5", "msg5"); adView.loadAd(adRequest); Log.i("6", "msg6"); } Log.i("7", "msg7"); convertView = adView; Log.i("8", "msg8"); } } else { Log.i("8", "msg8"); if (convertView == null || convertView instanceof AdView) { Log.i("9", "msg9"); convertView = inflater.inflate(R.layout.postitem, null); Log.i("10", "msg10"); viewHolder = new ViewHolder(); Log.i("11", "msg11"); viewHolder.postThumbView = (ImageView) convertView .findViewById(R.id.postThumb); Log.i("12", "msg12"); viewHolder.postTitleView = (TextView) convertView .findViewById(R.id.postTitleLabel); Log.i("13", "msg13"); viewHolder.postDateView = (TextView) convertView .findViewById(R.id.postDateLabel); Log.i("14", "msg14"); convertView.setTag(viewHolder); Log.i("15", "msg15"); } else { Log.i("16", "msg16"); viewHolder = (ViewHolder) convertView.getTag(); Log.i("17", "msg17"); } Log.i("18", "msg18"); if (datas[position].postThumbUrl == null) { Log.i("19", "msg19"); viewHolder.postThumbView .setImageResource(R.drawable.postthumb_loading); } Log.i("20", "msg20"); viewHolder.postTitleView.setText(datas[position].postTitle); viewHolder.postDateView.setText(datas[position].postDate); } Log.i("21", "msg21"); return convertView; 

    } }

    Error:

    03-16 21:34:10.509: I/1(415): msg1 03-16 21:34:10.530: I/2(415): msg2 03-16 21:34:10.530: I/3(415): msg3 03-16 21:34:10.530: I/4(415): msg4 03-16 21:34:10.582: I/5(415): msg5 03-16 21:34:10.609: D/dalvikvm(415): DexOpt: couldn't find field Landroid/content/res/Configuration;.smallestScreenWidthDp 03-16 21:34:10.609: W/dalvikvm(415): VFY: unable to resolve instance field 36 03-16 21:34:10.609: D/dalvikvm(415): VFY: replacing opcode 0x52 at 0x0012 03-16 21:34:10.623: D/dalvikvm(415): VFY: dead code 0x0014-0018 in
    Lcom/google/android/gms/common/GooglePlayServicesUtil;.b (Landroid/content/res/Resources;)Z 03-16 21:34:10.649: W/GooglePlayServicesUtil(415): Google Play Store is missing. 03-16 21:34:10.730: D/dalvikvm(415): DexOpt: --- BEGIN 'ads256706667.jar' (bootstrap=0) --- 03-16 21:34:10.919: D/dalvikvm(415): DexOpt: --- END 'ads256706667.jar' (success) --- 03-16 21:34:10.919: D/dalvikvm(415): DEX
    prep '/data/data/com.jms.admobinlistview/cache/ads256706667.jar': unzip in 2ms, rewrite 193ms 03-16 21:34:11.019: I/Ads(415): Use AdRequest.Builder.addTestDevice
    ("B3EEABB8EE11C2BE770B684D95219ECB") to get test ads on this device. 03-16 21:34:11.059: I/Ads(415): Starting ad request. 03-16 21:34:11.179: D/dalvikvm(415): GC_EXTERNAL_ALLOC freed 269K, 52% free 2677K/5575K,
    external 2032K/2137K, paused 74ms 03-16 21:34:11.499: I/6(415): msg6 03-16 21:34:11.499: I/7(415): msg7 03-16 21:34:11.499: I/8(415): msg8 03-16 21:34:11.509: I/21(415): msg21 03-16 21:34:11.509: D/AndroidRuntime(415): Shutting down VM 03-16 21:34:11.509: W/dalvikvm(415): threadid=1: thread exiting with uncaught exception
    (group=0x40015560) 03-16 21:34:11.589: E/AndroidRuntime(415): FATAL EXCEPTION: main 03-16 21:34:11.589: E/AndroidRuntime(415): java.lang.NullPointerException 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.AbsListView.obtainView
    (AbsListView.java:1432) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.ListView.makeAndAddView
    (ListView.java:1745) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.ListView.fillDown
    (ListView.java:670) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.ListView.fillFromTop
    (ListView.java:727) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.ListView.layoutChildren
    (ListView.java:1598) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.AbsListView.onLayout
    (AbsListView.java:1260) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.View.layout(View.java:7175) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.setChildFrame
    (LinearLayout.java:1254) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.layoutVertical
    (LinearLayout.java:1130) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.onLayout (LinearLayout.java:1047) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.View.layout(View.java:7175) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.FrameLayout.onLayout (FrameLayout.java:338) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.View.layout(View.java:7175) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.setChildFrame
    (LinearLayout.java:1254) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.layoutVertical (LinearLayout.java:1130) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.LinearLayout.onLayout
    (LinearLayout.java:1047) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.View.layout(View.java:7175) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.widget.FrameLayout.onLayout (FrameLayout.java:338) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.View.layout(View.java:7175) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.ViewRoot.performTraversals
    (ViewRoot.java:1140) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.view.ViewRoot.handleMessage (ViewRoot.java:1859) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.os.Handler.dispatchMessage (Handler.java:99) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.os.Looper.loop(Looper.java:123) 03-16 21:34:11.589: E/AndroidRuntime(415): at android.app.ActivityThread.main
    (ActivityThread.java:3683) 03-16 21:34:11.589: E/AndroidRuntime(415): at java.lang.reflect.Method.invokeNative(Native Method) 03-16 21:34:11.589: E/AndroidRuntime(415): at java.lang.reflect.Method.invoke(Method.java:507) 03-16 21:34:11.589: E/AndroidRuntime(415): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839) 03-16 21:34:11.589: E/AndroidRuntime(415): at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:597) 03-16 21:34:11.589: E/AndroidRuntime(415): at dalvik.system.NativeStart.main(Native Method) 03-16 21:34:17.879: I/Process(415): Sending signal. PID: 415 SIG: 9

Original Code with previous version of adMob:

/** * PostItemAdapter.java * * Adapter Class which configs and returns the View for ListView * */ package com.jms.admobinlistview;

import java.util.HashSet; import java.util.Set;

import android.app.Activity; import android.content.Context; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.ArrayAdapter; import android.widget.ImageView; import android.widget.TextView;

import com.google.ads.AdRequest; import com.google.ads.AdSize; import com.google.ads.AdView;

public class PostItemAdapter extends ArrayAdapter { private LayoutInflater inflater; private PostData[] datas; private Activity mainActivity; private AdView adView;

  public PostItemAdapter(Context context, int textViewResourceId, PostData[] objects) { super(context, textViewResourceId, objects); // TODO Auto-generated constructor stub mainActivity = (Activity) context; inflater = mainActivity.getLayoutInflater(); datas = objects; } static class ViewHolder { TextView postTitleView; TextView postDateView; ImageView postThumbView; } public View getView(int position, View convertView, ViewGroup parent) { ViewHolder viewHolder; if (position == 0) { if (convertView == null || !(convertView instanceof AdView)) { if (adView == null) { adView = new AdView(mainActivity, AdSize.SMART_BANNER, "a151bd35eeb068d"); AdRequest adRequest = new AdRequest(); Set<String> keywordsSet = new HashSet<String>(); keywordsSet.add("game"); keywordsSet.add("dating"); keywordsSet.add("money"); keywordsSet.add("girl"); adRequest.addKeywords(keywordsSet); adView.loadAd(adRequest); } convertView = adView; } } else { if (convertView == null || convertView instanceof AdView) { convertView = inflater.inflate(R.layout.postitem, null); viewHolder = new ViewHolder(); viewHolder.postThumbView = (ImageView) convertView .findViewById(R.id.postThumb); viewHolder.postTitleView = (TextView) convertView .findViewById(R.id.postTitleLabel); viewHolder.postDateView = (TextView) convertView .findViewById(R.id.postDateLabel); convertView.setTag(viewHolder); } else { viewHolder = (ViewHolder) convertView.getTag(); } if (datas[position].postThumbUrl == null) { viewHolder.postThumbView .setImageResource(R.drawable.postthumb_loading); } viewHolder.postTitleView.setText(datas[position].postTitle); viewHolder.postDateView.setText(datas[position].postDate); } return convertView; } 

}

===============================================================================

Tim, thanks for getting back to me. The following is my code and error message that I'm getting. I tried several things and I keep getting the same type of errors. Can you please clarify your explanation you gave? Is purview really preview?

package com.robertrichardson.bxxxyxxxxzxxxxLite;

import java.util.ArrayList; import java.util.HashSet; import java.util.Set;

import android.annotation.TargetApi; import android.content.Context; import android.content.Intent; import android.database.DataSetObserver; import android.net.ConnectivityManager; import android.os.Build; import android.os.Bundle; import android.support.v4.content.Loader; import android.text.format.DateFormat; import android.util.Log; import android.view.ContextMenu; import android.view.ContextMenu.ContextMenuInfo; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.AbsListView; import android.widget.AbsListView.MultiChoiceModeListener; import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.ImageButton; import android.widget.LinearLayout; import android.widget.ListView; import android.widget.RelativeLayout; import android.widget.TextView;

import com.actionbarsherlock.app.SherlockListFragment; import com.actionbarsherlock.view.MenuInflater; import android.app.Activity;

import com.google.android.gms.ads.AdListener; import com.google.android.gms.ads.AdRequest; import com.google.android.gms.ads.AdRequest.Builder; import com.google.android.gms.ads.AdSize; import com.google.android.gms.ads.AdView; import com.google.android.gms.ads.doubleclick.PublisherAdRequest; import com.google.android.gms.ads.doubleclick.PublisherAdView; import com.google.android.gms.drive.query.internal.InFilter; import com.robertrichardson.bxxxyxxxxzxxxxLite.R.string;

/* * BxxxxListFragment Class: Sets up and displays all Bxxxx Activity on the list. */

public class BxxxxListFragment extends SherlockListFragment {

  private ArrayList<Bxxxx> mBxxxx; private boolean mSubtitleVisible; private LayoutInflater inflater; private Activity mainActivity; private static final String AD_UNIT_ID = "ca-app-pub-4409647944042499/8301167762"; public static final String TAG = "BxxxxListFragment"; 

// private AdView adView; private AdView adView;

  @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); /* * New ads Logic */ setHasOptionsMenu(true); getSherlockActivity().setTitle(R.string.app_name); mBxxxx = BxxxxLab.get(getSherlockActivity()).getBxxxx(); BxxxxAdapter adapter = new BxxxxAdapter(mBxxxx); setListAdapter(adapter); setRetainInstance(true); mSubtitleVisible = false; } @Override public void onViewCreated(View view, Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); setEmptyText(getResources().getString(string.new_bxxxx_attack_empty_desc)); registerForContextMenu(getListView()); setHasOptionsMenu(true); } @TargetApi(11) @Override public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle savedInstanceState) { View v = super.onCreateView(inflater, parent, savedInstanceState); if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { if (mSubtitleVisible) { getSherlockActivity().getSupportActionBar().setSubtitle(R.string.subtitle); } } ListView listView = (ListView)v.findViewById(android.R.id.list); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.HONEYCOMB) { // Use floating point context menus on Froyer and Gingerbread registerForContextMenu(listView); } else { // Use contextual action bar on Honeycomb and higher listView.setChoiceMode(AbsListView.CHOICE_MODE_MULTIPLE); listView.setMultiChoiceModeListener(new MultiChoiceModeListener() { public boolean onActionItemClicked( android.view.ActionMode arg0, android.view.MenuItem arg1) { // TODO Auto-generated method stub return false; } public boolean onCreateActionMode(android.view.ActionMode mode, android.view.Menu menu) { // TODO Auto-generated method stub return false; } public void onDestroyActionMode(android.view.ActionMode mode) { // TODO Auto-generated method stub } public boolean onPrepareActionMode( android.view.ActionMode mode, android.view.Menu menu) { // TODO Auto-generated method stub return false; } public void onItemCheckedStateChanged( android.view.ActionMode mode, int position, long id, boolean checked) { // TODO Auto-generated method stub } // ActionMode.Callback methods }); } return v; 

}

  @Override public void onListItemClick(ListView l, View v, int position, long id) { Bxxxx c = ((BxxxxAdapter)getListAdapter()).getItem(position); // Start Bxxxx Activity Intent i = new Intent(getSherlockActivity(), BxxxxPagerActivity.class); i.putExtra(BxxxxFragment.EXTRA_BULLIED_ID, c.getId()); startActivityForResult(i, 0); } @Override public void onActivityResult(int requestCode, int resultCode, Intent data) {> ((BxxxxAdapter)getListAdapter()).notifyDataSetChanged(); } @Override public void onCreateOptionsMenu(com.actionbarsherlock.view.Menu menu, MenuInflater inflater) { super.onCreateOptionsMenu(menu, inflater); inflater.inflate(R.menu.fragment_bxxxx_list, menu); } @TargetApi(11) @Override public boolean onOptionsItemSelected(com.actionbarsherlock.view.MenuItem item) { switch (item.getItemId()) { case R.id.settings: Intent i=new Intent(getSherlockActivity(), Preferences.class); startActivity(i); return(true); case R.id.about: i=new Intent(getSherlockActivity(),BxxxxContentActivity.class); i.putExtra(BxxxxContentActivity.EXTRA_FILE, "file:///android_asset/misc/about.html"); startActivity(i); return(true); case R.id.help: i=new Intent(getSherlockActivity(),BxxxxContentActivity.class); i.putExtra(BxxxxContentActivity.EXTRA_FILE, "file:///android_asset/misc/help.html"); startActivity(i); return(true); case R.id.disclaimer: i=new Intent(getSherlockActivity(),BxxxxContentActivity.class); i.putExtra(BxxxxContentActivity.EXTRA_FILE, "file:///android_asset/misc/disclaimer.html"); startActivity(i); return(true); case R.id.menu_item_new_bxxxx: Bxxxx bxxxx = new Bxxxx(); BxxxxLab.get(getSherlockActivity()).addBxxxx(bxxxx); i = new Intent(getSherlockActivity(), BxxxxPagerActivity.class); i.putExtra(BxxxxFragment.EXTRA_BULLIED_ID, bxxxx.getId()); startActivityForResult(i,0); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onCreateContextMenu(ContextMenu menu,View v, ContextMenuInfo menuInfo) { getActivity().getMenuInflater().inflate(R.menu.bxxxx_list_item_context, menu); } @Override public boolean onContextItemSelected(android.view.MenuItem item) { AdapterContextMenuInfo info = (AdapterContextMenuInfo)item.getMenuInfo(); int position = info.position; BxxxxAdapter adapter = (BxxxxAdapter)getListAdapter(); Bxxxx bxxxx = adapter.getItem(position); switch (item.getItemId()) { case R.id.menu_item_delete_bxxxx: BxxxxLab.get(getActivity()).deleteBxxxx(bxxxx); adapter.notifyDataSetChanged(); return true; } return super.onContextItemSelected(item); } private class BxxxxAdapter extends ArrayAdapter<Bxxxx> { public BxxxxAdapter(ArrayList<Bxxxx> bxxxx) { super(getSherlockActivity(), android.R.layout.simple_list_item_1, bxxxx); } @Override public View getView(int position, View convertView, ViewGroup parent) { 

/* * New ads Logic
*/
Log.i("1", "msg1");

  if (position == 0) { if (convertView == null || !(convertView instanceof AdView)) { if (adView == null) { Log.i("2", "msg2"); adView = new AdView(mainActivity); <=== Abends on this line Log.i("3", "msg3"); adView.setAdUnitId("ca-app-pub-4409647944042499/8301167762"); Log.i("4", "msg4"); adView.setAdSize(AdSize.BANNER); Log.i("5", "msg5"); // Convert the default layout parameters so that they play nice with // ListView. float density = mainActivity.getResources().getDisplayMetrics().density; int height = Math.round(AdSize.BANNER.getHeight() * density); AbsListView.LayoutParams params = new AbsListView.LayoutParams( AbsListView.LayoutParams.FILL_PARENT, height); adView.setLayoutParams(params); AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) .addTestDevice("TEST_DEVICE_ID") .build(); adView.loadAd(adRequest); } convertView = adView; return convertView; } return convertView; } else { if (convertView == null || convertView instanceof AdView) { // If we weren't given a view, inflate one if (convertView == null) { convertView = getSherlockActivity().getLayoutInflater() .inflate(R.layout.list_item_bxxxx, null); } // Enter the view of this Bxxxx Attack Bxxxx c = getItem(position); TextView titleTextView = (TextView)convertView.findViewById(R.id.bxxxx_list_item_titleTextView); titleTextView.setText(c.getTitle()); TextView dateTextView = (TextView)convertView.findViewById(R.id.bxxxx_list_item_dateTextView); dateTextView.setText(DateFormat.format("EEEE, MMM dd, yyyy hh : mm a", c.getDate()).toString()); CheckBox resolvedCheckBox = (CheckBox)convertView.findViewById(R.id.bxxxx_list_item_resolvedCheckBox); resolvedCheckBox.setChecked(c.isResolved()); return convertView; } return convertView; } } 

}

  @Override public void onResume() { super.onResume(); ((BxxxxAdapter)getListAdapter()).notifyDataSetChanged(); } } 

03-18 06:52:29.785: D/dalvikvm(932): VFY: replacing opcode 0x6f at 0x0006 03-18 06:52:30.085: I/1(932): msg1 03-18 06:52:30.085: I/2(932): msg2 03-18 06:52:30.085: D/AndroidRuntime(932): Shutting down VM 03-18 06:52:30.095: W/dalvikvm(932): threadid=1: thread exiting with uncaught exception (group=0x40015560) 03-18 06:52:30.135: E/AndroidRuntime(932): FATAL EXCEPTION: main 03-18 06:52:30.135: E/AndroidRuntime(932): java.lang.NullPointerException 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.ViewConfiguration.get(ViewConfiguration.java:228) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.(View.java:1878) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.ViewGroup.(ViewGroup.java:281) 03-18 06:52:30.135: E/AndroidRuntime(932): at com.google.android.gms.ads.AdView.(Unknown Source) 03-18 06:52:30.135: E/AndroidRuntime(932): at com.robertrichardson.bxxxyxxxxzxxxLite.BxxxxListFragment$BxxxxAdapter.getView(BxxxxListFragment.java:251) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.AbsListView.obtainView(AbsListView.java:1430) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.ListView.makeAndAddView(ListView.java:1745) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.ListView.fillDown(ListView.java:670) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.ListView.fillFromTop(ListView.java:727) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.ListView.layoutChildren(ListView.java:1598) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.AbsListView.onLayout(AbsListView.java:1260) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.FrameLayout.onLayout(FrameLayout.java:338) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.view.View.layout(View.java:7175) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.LinearLayout.setChildFrame(LinearLayout.java:1254) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.LinearLayout.layoutVertical(LinearLayout.java:1130) 03-18 06:52:30.135: E/AndroidRuntime(932): at android.widget.LinearLayout.onLayout(LinearLayout.java:1047)

It looks like you are hiding the class variable 'adView' in the new code by declaring a local variable of the same name. Due to the scope, you're setting convertView to the global adView, which is always null. Just change "AdView adView =" to "adView ="

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM