我正在开发包含Admob横幅的简单Android应用,但是当我运行该应用时,我收到此消息:
unfortunately the app has stopped
这是我的代码:
xml layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.ajater.mj.vaccalc.MainActivity"
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads">
<com.google.ads.AdView
xmlns:ads="http://schemas.android.com/apk/libs/com.google.ads"
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"
ads:adUnitId="------------------" />
</android.support.design.widget.CoordinatorLayout>
mainactivity
AdView adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="my.app.name">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
答案 0 :(得分:0)
我认为您需要在AdView上方添加此行
MobileAds.initialize(getApplicationContext(), "Your own admob ad-unit");
注意:请勿测试您真实的adob广告单元。如果是这样,有机会通过谷歌终止您的admob帐户。
答案 1 :(得分:0)
试试这段代码,
<com.google.android.gms.ads.AdView
android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_key">
</com.google.android.gms.ads.AdView>