如何将AdView从AdMob添加到ListView

时间:2011-04-01 23:33:39

标签: android admob

我很困惑如何使用listview从admob工作制作adview。我是广告新手,刚刚完成了基本教程,但现在尝试投入我自己的应用程序。我有一些注意到我试图开始的事情。

        // 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):致命异常:主要   04-01 19:28:58.163:ERROR / AndroidRuntime(421):   java.lang.RuntimeException:无法启动活动   ComponentInfo {org.demo.textaway2 / org.demo.textaway2.TextAway2}:   java.lang.UnsupportedOperationException:addView(View)不是   在AdapterView 04-01 19:28:58.163中支持:   错误/ 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(ActivityThread.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:   错误/ 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:   错误/ 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:   错误/ AndroidRuntime(421):引起:   java.lang.UnsupportedOperationException:addView(View)不是   在AdapterView 04-01 19:28:58.163中支持:   错误/ 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更多

1 个答案:

答案 0 :(得分:2)

堆栈跟踪包括:

addView(View) is not supported in AdapterView

这回答了您的问题 - 您无法在AdapterView或其子类中使用AdMob,而您正尝试这样做。您需要编写自己的View类来呈现列表对象,使用不同的广告服务(adwhirl等 - 或许另一个没有此限制)或将广告移动到列表之外,进入页脚/报头中。