我正在使用Android Studio 2.2 Preview 7开发 AdMob 应用。
当我在模拟器和移动设备上运行此应用程序时,它说:
不幸的是,Ads6 app停止了
为什么会这样?
这是我的x.calc(k)
文件:
MainActivity
这是我的XML文件:
public class MainActivity extends AppCompatActivity
{
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(
getApplicationContext(),
"ca-app-pub-xxxxxxxxxxxx/7xxxxxxxxxxxxx");
AdView madview=(AdView) this.findViewById(R.id.adView);
AdRequest adrequest=new AdRequest.Builder().build();
madview.loadAd(adrequest);
}
}
这是我的清单文件:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alpha.ads6.MainActivity">
<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="ca-app-pub-xxxxxxxxxxxx/xxxxxx">
</com.google.android.gms.ads.AdView>
答案 0 :(得分:0)
尝试这样:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.alpha.ads6.MainActivity">
<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="ca-app-pub-xxxxxxxxxxxx/xxxxxx">
</com.google.android.gms.ads.AdView>
使用xmlns:ads="http://schemas.android.com/apk/res-auto"
代替xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
评论MobileAds.initialize();
希望这有帮助。!
答案 1 :(得分:0)
我在我的项目中使用此代码并且工作正常。我希望它对你有所帮助。
活动代码
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
public class MainActivity extends Activity {
private static final String ADMOB_APP_ID = "ca-app-pub-###################~##########";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this,ADMOB_APP_ID);
AdView mAdView1 = (AdView) findViewById(R.id.adView1);
AdRequest adRequest1 = new AdRequest.Builder().build();
mAdView1.loadAd(adRequest1);
}
活动XML代码
<com.google.android.gms.ads.AdView
android:id="@+id/adView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adSize="BANNER"
ads:adUnitId="@string/banner_ad_unit_1"
android:layout_alignParentStart="true"
android:layout_marginBottom="46dp">
</com.google.android.gms.ads.AdView>
字符串XML代码
<resources>
<string name="banner_ad_unit_1">ca-app-pub-################/########</string>
</resources>