How to add AdView from AdMob to a ListView
I'm confused at how to make adview from admob work with a listview. I'm new to ads and have just finished the basic tutorial but now trying to put in in my own app. I have some things commented out that I tried to do to begin with.
// Create the adView
AdView adView = new AdView(this, AdSize.BANNER, myID);
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="@+id/mainLayout"
// LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout);
// Add the adView to it
lv.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());
<TextView android:id="@+id/itemName" android:layout_width="wrap_content"
android:layout_height="50dip" android:text="Test view"
android:textSize = "25sp"
android:singleLine = "true"/>
04-01 19:28:58.163: ERROR/AndroidRuntime(421): FATAL EXCEPTION: main 04-01 19:28:58.163: ERROR/AndroidRuntime(421): java.lang.RuntimeException: Unable to start activity ComponentInfo{org.demo.textaway2/org.demo.textaway2.TextAway2}: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread.access$2300(ActivityThread.java:125) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread$H.handleMessage(ActivityTh开发者_JAVA技巧read.java:2033) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.os.Handler.dispatchMessage(Handler.java:99) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.os.Looper.loop(Looper.java:123) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread.main(ActivityThread.java:4627) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at java.lang.reflect.Method.invokeNative(Native Method) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at java.lang.reflect.Method.invoke(Method.java:521) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at dalvik.system.NativeStart.main(Native Method) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): Caused by: java.lang.UnsupportedOperationException: addView(View) is not supported in AdapterView 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.widget.AdapterView.addView(AdapterView.java:435) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at org.demo.textaway2.TextAway2.onCreate(TextAway2.java:164) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627) 04-01 19:28:58.163: ERROR/AndroidRuntime(421): ... 11 more
The stack trace includes this:
addView(View) is not supported in AdapterView
Which answers your question - you can't use AdMob in an AdapterView or its subclasses, which you're trying to do. You'll either need to write your own View class to render the list objects, use a different ad service (adwhirl etc - perhaps another doesn't have this limitation) or move the ad(s) outside the list, into a footer/header.
精彩评论