我正处于为Android开发游戏的早期阶段,我正在尝试使用AdMob合并广告横幅。我已经从官方site的教程中直接下载了样本,所以我想无论我在这里做错什么都必须是基本的,因为它在我的Galaxy S2设备上调试几秒后就会崩溃。请帮助。
package com.google.example.ads.fundamentals;
import com.google.ads.AdRequest;
import com.google.ads.AdSize;
import com.google.ads.AdView;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.telephony.TelephonyManager;
import android.widget.LinearLayout;
/**
* A simple {@link Activity} that embeds an AdView.
*/
public class BannerSample extends Activity {
private AdView adView;
private final TelephonyManager tm =
(TelephonyManager)getBaseContext().getSystemService(Context.TELEPHONY_SERVICE);
private final String AD_MOB_ID = "my AdMob ID goes here";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// Create an ad.
adView = new AdView(this, AdSize.BANNER, AD_MOB_ID);
// Add the AdView to the view hierarchy. The view will have no size
// until the ad is loaded.
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout);
layout.addView(adView);
AdRequest adRequest = new AdRequest();
adRequest.addTestDevice(tm.getDeviceId());
// Start loading the ad in the background.
adView.loadAd(adRequest);
}
/** Called before the activity is destroyed. */
@Override
public void onDestroy() {
// Destroy the AdView.
if (adView != null) {
adView.destroy();
}
super.onDestroy();
}
}
屏幕截图中的Logcat数据为here
编辑:还添加了Manifest.XML,我怀疑是造成这些问题的原因 - 令人惊讶的是官方网站上的示例附带的一个错误(根据Eclipse),所以我不得不修改它一点:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapp"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="3"
android:targetSdkVersion="13" />
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name">
<activity
android:name=".HelloAdMobActivity"
android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
android:screenOrientation="landscape" >
</activity>
</application>
</manifest>
答案 0 :(得分:1)
Logcat信息非常有用,但我打赌其中一个原因是:
libs/
文件夹中。您有两个选项来解决此问题:将其添加到libs/
文件夹,或转到属性 - &gt; Java构建路径 - &gt;订购和导出并检查AdMob jar。android:id="linearLayout"
的LinearLayout。由于示例项目应该包括此项,因此这种可能性较小。答案 1 :(得分:1)
好吧,事实证明问题出在tm.getDeviceId()
,无论出于什么原因导致程序崩溃。
答案 2 :(得分:0)
您需要添加以下权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>