Java Android studio-谷歌广告崩溃应用程序

Java Android studio-谷歌广告崩溃应用程序,java,android,gradle,ads,Java,Android,Gradle,Ads,我试图在我的应用程序中实现谷歌广告服务,但运气不好 我曾尝试更改应用程序的sdk版本和google ads实现版本,每次应用程序在启动时崩溃,我都不知道我做错了什么,非常感谢您的帮助 以下是我遵循的STPE: 谷歌广告的依赖关系: my activity_main.xml中的AdView: 我还升级到了sdk 28(WAS27),使用了“迁移到AndroidX按钮”来实现 这就是我的应用程序gradle的样子: apply plugin: 'com.android.application' ap

我试图在我的应用程序中实现谷歌广告服务,但运气不好

我曾尝试更改应用程序的sdk版本和google ads实现版本,每次应用程序在启动时崩溃,我都不知道我做错了什么,非常感谢您的帮助

以下是我遵循的STPE:

  • 谷歌广告的依赖关系:
  • my activity_main.xml中的AdView:
  • 我还升级到了sdk 28(WAS27),使用了“迁移到AndroidX按钮”来实现

    这就是我的应用程序gradle的样子:

    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.ofekTe.MyWorkouts"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    repositories {
        maven { url 'https://jitpack.io' }
        mavenCentral()
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
        implementation 'com.google.android.material:material:1.2.0-alpha03'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
        implementation 'com.google.code.gson:gson:2.8.2'
        implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
        implementation 'com.miguelcatalan:materialsearchview:1.4.0'
        implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
        implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    
        implementation 'com.google.android.gms:play-services-ads:18.3.0'
    
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.3.0-alpha03'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha03'
    }
    
    这是我得到的日志(至少重要的部分,如果缺少任何东西,请务必告诉我,我将上传整个日志):


    再次感谢您的帮助。谢谢

    我认为您缺少AndroidManifest.xml文件中所需的代码

  • 谷歌广告的依赖关系:

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

  • 在AndroidManifest.xml文件中添加以下“元数据”代码 创建应用程序ID并使用它,否则在进行测试时,可以使用示例AdMob应用程序ID代替“ca-App-pub-XXXXXXXXXXXXXX~YYYYYY”

    我用上面的代码创建了一个新的应用程序,它显示了带有测试广告的横幅,如下所示


  • 我的应用程序也有同样的问题,我必须向我的应用程序清单文件添加一些元数据

    <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>
    

            MobileAds.initialize(this, new OnInitializationCompleteListener() {
                @Override
                public void onInitializationComplete(InitializationStatus initializationStatus) {
                    AdView mAdView = (AdView)findViewById(R.id.adView);
                    AdRequest adRequest = new AdRequest.Builder().build();
                    mAdView.loadAd(adRequest);
                }
            });
    
    apply plugin: 'com.android.application'
    apply plugin: 'kotlin-android'
    
    android {
        compileSdkVersion 28
        defaultConfig {
            applicationId "com.ofekTe.MyWorkouts"
            minSdkVersion 21
            targetSdkVersion 28
            versionCode 1
            versionName "1.0"
            testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
        }
        buildTypes {
            release {
                minifyEnabled false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }
    
    repositories {
        maven { url 'https://jitpack.io' }
        mavenCentral()
    }
    
    dependencies {
        implementation fileTree(include: ['*.jar'], dir: 'libs')
        implementation 'androidx.appcompat:appcompat:1.2.0-alpha01'
        implementation 'com.google.android.material:material:1.2.0-alpha03'
        implementation 'androidx.legacy:legacy-support-v4:1.0.0'
        implementation 'androidx.constraintlayout:constraintlayout:2.0.0-beta4'
        implementation 'com.google.code.gson:gson:2.8.2'
        implementation 'com.github.PhilJay:MPAndroidChart:v3.1.0'
        implementation 'com.miguelcatalan:materialsearchview:1.4.0'
        implementation 'com.github.amlcurran.showcaseview:library:5.4.3'
        implementation 'com.readystatesoftware.sqliteasset:sqliteassethelper:2.0.1'
    
        implementation 'com.google.android.gms:play-services-ads:18.3.0'
    
        testImplementation 'junit:junit:4.12'
        androidTestImplementation 'androidx.test:runner:1.3.0-alpha03'
        androidTestImplementation 'androidx.test.espresso:espresso-core:3.3.0-alpha03'
    }
    
    <manifest>
    <application>
        <!-- Sample AdMob App ID: ca-app-pub-3940256099942544~3347511713 -->
        <meta-data
            android:name="com.google.android.gms.ads.APPLICATION_ID"
            android:value="ca-app-pub-xxxxxxxxxxxxxxxx~yyyyyyyyyy"/>
    </application>
    
    <com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    app:adSize="BANNER"
    app:adUnitId="ca-app-pub-3940256099942544/6300978111"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"></com.google.android.gms.ads.AdView>
    
    public class MainActivity extends AppCompatActivity {
    
         private AdView mAdView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
               mAdView = findViewById(R.id.adView);
               AdRequest adRequest = new AdRequest.Builder().build();
               mAdView.loadAd(adRequest);
        }}
    
    <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>