Why does my Android Activity close when AdMob loads an ad?

别来无恙 提交于 2020-01-04 15:27:08

问题


I've been putting AdMob ads in an otherwise working Android app, following the instructions here. The app has 2 activities. One of them is only reachable from the first. I'm currently adding ads to this second activity. I've tried both creating an AdView programmatically in the onCreate method and adding appropriate code to the XML layout file for the Activity. In both cases the activity will exit the second Activity and go to the first as if the back button had been pressed as soon as it recieves an ad (or at least some ad data over the network). If I change the code so as to not make an add request (even if I add the ad programatically but just don't to call loadAd), or if I turn off my netwrok access then I don't see any ads, but my Activity doesn't exit either. What could be causing this?

I've gone through all the (I believe out of date) steps here, but that doesn't help. Just to be clear in case I did something wrong, I have:

  • Downloaded the latest Android and AdMob SDKs, at least as of last Friday.
  • Set 'target=android-17' in default.properties (which I had to create in the root of the project, since I couldn't fine) and project.properties (which is what I think the project actually uses).
  • Set 'android:minSdkVersion="3"' in AndroidManifest.xml.
  • Added the library to the project (by placing GoogleAdMobAdsSdk-6.2.1.jar in a libs directory in the project root).
  • Added a com.google.ads.AdActivity copied from here to AndroidManifest.xml.
  • Added permission to AndroidManifest.xml, again copied from the same place.
  • Copied both code and XML tags (and attributes) from here and added them to my project (replacing MY_AD_UNIT_ID with my publisher ID), although I also broke out the request in code to make the emulator a test device.
  • Added an attrs.xml with ad styles. I'm fairly certain this is obsolete advice, but I did it anyway. I used the one from this question, although I changed com.admob.android.ads.AdView to com.google.ads.

The next thing I think you'll want to know if you try to help me is what LogCat says. Not much:

09-02 14:33:34.861: I/Ads(12839): adRequestUrlHtml: <html><head><script src="http://media.admob.com/sdk-core-v40.js"></script><script>AFMA_getSdkConstants();AFMA_buildAdURL({"msid":"com.Package.Name","hl":"en","bas_off":0,"simulator":1,"preqs":0,"slotname":"a150cbaa53d9d3d","js":"afma-sdk-a-v6.2.1","isu":"B3EEABB8EE11C2BE770B684D95219ECB","u_audio":4,"cap":"m,a","cipa":0,"u_sd":1.5,"net":"ed","u_h":533,"oar":0,"session_id":"4834854664722735718","seq_num":"1","app_name":"1.android.com.Package.Name","bas_on":0,"kw":[],"u_w":320,"gnt":3,"adtest":"on","format":"320x50_mb","carrier":"310260","ad_pos":{"visible":0,"width":0,"height":0,"x":0,"y":0},"ptime":0});</script></head><body></body></html>
09-02 14:33:35.111: D/dalvikvm(12839): GC freed 8784 objects / 437720 bytes in 64ms
09-02 14:33:37.101: I/Ads(12839): Received ad url: <url: "http://googleads.g.doubleclick.net:80/mads/gma?msid=com.Package.Name&hl=en&bas_off=0&preqs=0&js=afma-sdk-a-v6.2.1&isu=B3EEABB8EE11C2BE770B684D95219ECB&u_audio=4&cap=m%2Ca&cipa=0&u_sd=1.5&net=ed&u_h=533&oar=0&session_id=4834854664722735718&seq_num=1&app_name=1.android.com.Package.Name&bas_on=0&kw&u_w=320&gnt=3&adtest=on&format=320x50_mb&carrier=310260&ptime=0&u_so=p&output=html&region=mobile_app&u_tz=0&client_sdk=1&ex=1&slotname=a14e8f77524dde8&kw_type=broad&gsb=3g&caps=interactiveVideo_th_autoplay_mediation_sdkAdmobApiForAds_di&jsv=41" type: "admob" afmaNotifyDt: "null" useWebViewLoadUrl: "false">
09-02 14:33:37.111: I/Ads(12839): Request scenario: Online server request.
09-02 14:33:38.061: I/jdwp(12861): received file descriptor 10 from ADB
09-02 14:33:38.091: D/ddm-heap(12861): Got feature list request

EDIT: I don't think it will be very useful, but at the request of suleman khan here is my current XML

<com.google.ads.AdView android:layout_width="wrap_content"
    android:layout_height="wrap_content" ads:adUnitId="a150cbaa53d9d3d"
    ads:adSize="SMART_BANNER" android:id="@+id/adView" ads:refreshInterval="60" 
    ads:testDevices="TEST_EMULATOR"
    ads:loadAdOnCreate="true" />

I"ve tried many variations of this, with no success.


回答1:


Place this code in your xml:

<com.google.ads.AdView android:id="@+id/adView"
                         android:layout_width="wrap_content"
                         android:layout_alignParentTop="true"
                         android:layout_height="wrap_content"
                         ads:adUnitId="@string/my_publisher_id"
                         ads:adSize="SMART_BANNER"
                         ads:loadAdOnCreate="true"/>

Hope you have added your publisher id.

Place this after your activity in Manifest file:

<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

As you said that you have added jar file in libs in your project and hope you have also configured it in your buildpath.

Hope this helps.




回答2:


above code will work, and you can remove these tags also

    android:minSdkVersion="8"
    android:targetSdkVersion="8"


来源:https://stackoverflow.com/questions/13946976/why-does-my-android-activity-close-when-admob-loads-an-ad

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!