我的Android Studio版本为3.4.1,我正在尝试将MobileAds代码添加到使用sdk 28构建的项目中,但出现崩溃报告。我不知道为什么???

我检查了所有类似的帖子,但其中一些已过时,很遗憾无法获得任何解决方案

我的Android清单是

<meta-data
    android:name="com.google.android.gms.ads.AD_MANAGER_APP"
    android:value="true"/>

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="ca-app-pub-3940256099942544/6300978111"/>
        <activity
    android:name="com.google.android.gms.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
    android:screenOrientation="portrait" />


MainActivity布局

    <com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_weight="1"
ads:adSize="SMART_BANNER"
ads:adUnitId="ca-app-pub-3940256099942544/6300978111">




应用程式build.gradle

   android {
compileSdkVersion 28
buildToolsVersion '28.0.3'
defaultConfig {
minSdkVersion 19
targetSdkVersion 28
multiDexEnabled true
buildTypes {
release {
    minifyEnabled false
    proguardFiles getDefaultProguardFile('proguard-android.txt'),
'proguard-rules.txt'
}
}
repositories {
jcenter()
mavenCentral()  // GPUImage for Android
}
dexOptions {
javaMaxHeapSize "4g"
}
productFlavors {
}
}
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation files('libs/acra-4.5.0.jar')
implementation files('libs/ormlite-android-4.43.jar')
implementation files('libs/ormlite-core-4.43.jar')
// If you want to use the GPU Filters
implementation 'com.android.support:support-v4:28.0.0'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:design:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
 implementation 'com.google.firebase:firebase-ads:17.2.1'
implementation 'com.google.firebase:firebase-messaging:18.0.0'
implementation 'com.loopj.android:android-async-http:1.4.9'
implementation 'com.github.bumptech.glide:glide:4.9.0'
implementation 'jp.wasabeef:glide-transformations:2.0.1'
implementation 'com.onesignal:OneSignal:3.10.9'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:animated-vector-drawable:28.0.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:exifinterface:28.0.0'
implementation 'com.google.android.gms:play-services-ads:17.2.1'
implementation 'com.google.android.gms:play-services-maps:+'

}
apply plugin: 'com.google.gms.google-services'


HomeActivity.java

    @Override
 protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_new);
MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");

AdView mAdView = (AdView) findViewById(R.id.adView);
try{

    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    if (admov_status.equalsIgnoreCase("true")){
        mAdView.setVisibility(View.VISIBLE);
    }else if(admov_status.equalsIgnoreCase("false")) {
        mAdView.setVisibility(View.GONE);
    }
}catch (RuntimeException e){
    mAdView.setVisibility(View.GONE);
}
}


但是错误仍然在运行。错误logcat详细信息在这里

java.lang.RuntimeException: Unable to get provider com.google.android.gms.ads.MobileAdsInitProvider: java.lang.IllegalStateException:

******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************


    at android.app.ActivityThread.installProvider(ActivityThread.java:6242)
    at android.app.ActivityThread.installContentProviders(ActivityThread.java:5805)
    at android.app.ActivityThread.handleBindApplication(ActivityThread.java:5722)
    at android.app.ActivityThread.-wrap1(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1656)
    at android.os.Handler.dispatchMessage(Handler.java:106)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6494)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:807)
 Caused by: java.lang.IllegalStateException:

******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************


    at com.google.android.gms.internal.ads.zzabh.attachInfo(Unknown Source:19)
    at com.google.android.gms.ads.MobileAdsInitProvider.attachInfo(Unknown Source:3)
    at android.app.ActivityThread.installProvider(ActivityThread.java:6239)
        ... 10 more


谁能帮我解决问题。

最佳答案

Admob中有2个广告ID。

1)。 appID

2)。 adUnitID

对于一个应用程序,无论您使用哪种广告类型,其appID都是相同的。但是adUnitID对于您使用的每种广告类型都是不同的。例如,您可以在应用程序中同时包含横幅广告和插页式广告。对于这两种广告类型,appID相同,但是adUnitID不同。

根据您的代码,您到处都使用了相同的ID,这是不正确的。您需要在适当的位置使用appID和adUnitID。在布局xml中,您在其中声明广告单元的位置应具有adUnitID,其他位置应具有appID。

Manifest.xml

<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value=**appID**/>


MainActivity布局

<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="bottom"
android:layout_weight="1"
ads:adSize="SMART_BANNER"
ads:adUnitId=**adUnitID**>


HomeActivity.java

MobileAds.initialize(this, **appID**);


因此,在您的情况下,似乎您已将adUnitID用于应该使用appID的位置。这可能就是为什么它会引发如下错误

******************************************************************************
* Invalid application ID. Follow instructions here: Link to *
* find your app ID.                                                          *
******************************************************************************


有关如何找出adMob的appID和adUnitID的信息,请参考此link

关于java - Android 8.1无法获取提供商com.google.android.gms.ads.MobileAds !!! APK崩溃,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56557266/

10-12 05:57