-1

I want show banner ad in my android app but when I put code in .xml and class file app crashing.

Refer below code

XML file:

 <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:foregroundGravity="center"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

in Java file:

import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;

 private AdView mAdView;

 mAdView = findViewById(R.id.adView1);
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);

app gradel file dependencies:

implementation 'com.google.android.gms:play-services-ads:18.1.0'

Manifest file :

android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-3940256099942544/6300978111"
virusarthak
  • 314
  • 1
  • 3
  • 12
  • If app is crashing post the log. – sanjeev Aug 29 '19 at 05:13
  • Please share crashing error. – MehulGohil Aug 29 '19 at 05:14
  • When I put banner it showing this message in Run logs: 08/29 10:42:02: Launching 'app' on Xiaomi Redmi Note 5 Pro. $ adb shell am start -n "com.myapp.quoteshub/com.myapp.appname.MainActivity" -a android.intent.action.MAIN -c android.intent.category.LAUNCHER Waiting for process to come online... Timed out waiting for process to appear on xiaomi-redmi_note_5_pro-20049080. – Harshal Gadakh Aug 29 '19 at 05:15

2 Answers2

0

First of all, you don't need to test with a live account with real AdMob unit id, you can use test ad unit id. And don't need to check if it is in debug mode or not.

You can follow this way to integrate AdMob test banner ads.

  1. Add this in project-level build.gradle

    allprojects {
        repositories {
            google()
            jcenter()
        }
    }
    
  2. Add this app-level build.gradle

      dependencies {
         implementation 'com.google.android.gms:play-services-ads:17.1.1'
      }
    
  3. Then add this in Manifest.xml file inside application tag

      <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
      <meta-data
          android:name="com.google.android.gms.ads.APPLICATION_ID"
          android:value="[ADMOB_APP_ID]"/>
    
  4. Then In your layout XML file

    <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
    
  5. Then in your activity do this

    package ...
    
    import ...
    import com.google.android.gms.ads.AdRequest;
    import com.google.android.gms.ads.AdView;
    
    public class MainActivity extends AppCompatActivity {
        private AdView mAdView;
    
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            MobileAds.initialize(this,
                "ca-app-pub-3940256099942544~3347511713");
            mAdView = findViewById(R.id.adView);
            AdRequest adRequest = new AdRequest.Builder().build();
            mAdView.loadAd(adRequest);
        }
    }
    

Then it will work perfectly for all devices. After that you have to just replace your admob app id and banner unit id. Hope it will help you.

Koushik Mondal
  • 865
  • 7
  • 15
0

I had the same issue with my app, I had to add some meta-datas to my app manifest file

<application>
<meta-data
        android:name="com.google.android.gms.ads.AD_MANAGER_APP"
        android:value="true"/>
<meta-data
        android:name="com.google.android.gms.ads.com.example.appname"
        android:value="enter your AdMob App ID e.g. ca-app-pub-3940256099942544~3347511713"/>
</application>

This step is required as of Google Mobile Ads SDK version 17.0.0. Failure to add this tag results in a crash with the message: The Google Mobile Ads SDK was initialized incorrectly. for more info visit https://developers.google.com/admob/android/quick-start#update_your_androidmanifestxml

and https://developers.google.com/ad-manager/mobile-ads-sdk/android/quick-start#update_your_androidmanifestxml