Google play services ads and AndroidManifest.xml

edited January 2017 in Android Mode

Hi all!

I have managed to get the google play services lib included with your help, but when I run my app with the adview code, it crashes on startup. "Unfortunately, app has stopped." I think there is something missing in my AndroidManifest.xml file, but I do not know what it is and can't seem to find applicable posts online.

My AndroidManifest.xml is as follows:

<?xml version="1.0" encoding="UTF-8"?>
<manifest xmlns:android="http:// schemas.android.com/apk/res/android" android:versionCode="1" android:versionName="1.0" package="">
  <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="25"/>
  <application android:debuggable="true" android:icon="@drawable/icon" android:label="">
    <activity android:name=".MainActivity">android:theme="@android:style/Theme.NoTitleBar"&gt;<intent-filter>
        <action android:name="android.intent.action.MAIN"/>
        <category android:name="android.intent.category.LAUNCHER"/>
      </intent-filter>
    </activity>
    <activity android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize" android:name="com.google.android.gms.ads.AdActivity"/>
    <meta-data android:name="com.google.android.gms.version" android:value="8115000"/>
  </application>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
  <uses-permission android:name="android.permission.ACCESS_WIFI_STATE"/>
  <uses-permission android:name="android.permission.INTERNET"/>
</manifest>

and the code to load ads is

@ Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   Window window = getActivity().getWindow();
   RelativeLayout adsLayout = new RelativeLayout(this.getActivity());
   RelativeLayout.LayoutParams lp2 = new RelativeLayout.LayoutParams(
          RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.FILL_PARENT);
   // Displays Ads at the bottom of your sketch, use Gravity.TOP to display them at the top
   adsLayout.setGravity(Gravity.BOTTOM);
   AdView adView = new AdView(this.getActivity());
   adView.setAdSize(AdSize.BANNER);
   adView.setAdUnitId("adunitid");
   adsLayout.addView(adView);
    AdRequest newAdReq = new AdRequest.Builder()

    .build();

    adView.loadAd(newAdReq);
    window.addContentView(adsLayout,lp2);
}

If I comment out the line adView.loadAd(newAdReq); the app works just fine

Answers

  • @Sayid=== not enough time for testing your code...Yet, your manifest seems ok; problem is with your line14, when you create an adRequest without indicating your test device id; other problem could be with your "adunitid" , but i suppose it's defined elsewhere like a constant.

  • edited January 2017

    The adunitid is "adunitid" in the example just to mask my real ad unit ID. How would I create a request with the device id?

    AdRequest newAdReq = new AdRequest.Builder().addTestDevice("AC98C820A50B4AD8A2106EDE96FB87D4").build();

    doesn't work either

  • @Sayid===

              final TelephonyManager tm =(TelephonyManager)getActivity().getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
    
            String deviceid = tm.getDeviceId();
    
  • edited January 2017

    Context isn't known so I replaced it with this.getActivity() and it seemed to be okay with the environment:

    final TelephonyManager tm =(TelephonyManager)getActivity().getBaseContext().getSystemService(this.getActivity().TELEPHONY_SERVICE);
    
       String deviceid = tm.getDeviceId();
    

    but this too crashes the app? I do not think this is the problem because isn't the adrequest supposed to load live ads if you don't give it the testdevice id?

  • Answer ✓

    I found the deviceid through logcat and it showed the correct android.gms.version too so i updated those and it works now! Thank you my friends

  • Answer ✓

    @Sayid=== put answered for others...

Sign In or Register to comment.