我正处于为Android开发游戏的早期阶段,我正试图使用AdMob整合广告横幅。我直接从官方网站上的教程中下载了这个示例,所以我想无论我在这里做错了什么,都必须是基本的,因为在我的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数据在这里
编辑:还添加了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>
Logcat信息将非常有用,但我打赌这两个原因之一:
- 您将SDK引用为外部Jar,但没有将其添加到
libs/
文件夹中。这里有两个选项可以解决这个问题:将其添加到libs/
文件夹中,或者转到Properties->Java Build Path->Order and Export并检查AdMob jar - 使用
android:id="linearLayout"
的XML中没有LinearLayout。这是不太可能的,因为样本项目应该包括这一点
好吧,原来问题出在tm.getDeviceId()
上,无论出于什么原因,它都会导致程序崩溃。
您需要添加以下权限。
<uses-permission android:name="android.permission.READ_PHONE_STATE"/>