我想在我的测试应用程序中集成admob代码,下面是我遵循的步骤。
布局文件看起来像这个
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="bottom"
android:id="@+id/layout_ad">
</LinearLayout>
清单文件看起来像这个
<?xml version="1.0" encoding="utf-8"?>
<uses-sdk android:minSdkVersion="8" />
<uses-permission android:name="android.permission.INTERNET" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name" >
<activity
android:name=".Dummyads2Activity"
android:label="@string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
最后是代码的外观
public class Dummyads2Activity extends Activity {//implements AdWhirlInterface{
/** Called when the activity is first created. */
private AdWhirlLayout adWhirlLayout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
try{
AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
LinearLayout layout = (LinearLayout)findViewById(R.id.layout_ad);
AdWhirlLayout adWhirlLayout = new AdWhirlLayout(this, "my adwhirl id");
Display d = this.getWindowManager().getDefaultDisplay();
RelativeLayout.LayoutParams adWhirlLayoutParams = new RelativeLayout.LayoutParams(d.getWidth(), 72);
layout.addView(adWhirlLayout, adWhirlLayoutParams);
}catch(Exception e){
//Log.e(TAG, "Unable to create AdWhirlLayout", e);
}
}
如果我这样做,我的应用程序就会崩溃,不会出现广告。请帮我解决这个问题。
这实际上取决于您使用的AdMob和AdWhirl的版本。我将讨论AdMob SDK v6.1.0和AdWhirl v3.2.0,它们是目前最新的版本。
你崩溃可能是因为这条线:
AdManager.setTestDevices( new String[] { AdManager.TEST_EMULATOR } );
这看起来像是传统的AdMob AdManager;AdWhirl使用Google AdMob SDK。无论如何,如果你想用AdWhirl测试广告,你想通过AdWhirr设置测试,而不是通过广告网络。因此,删除上面的行并设置测试模式如下:
AdWhirlTargeting.setTestMode(true);
此外,你的清单需要有一个超集,包含你正在调解的网络的所有权限和活动定义。对于AdMob,您需要添加:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
...
<activity android:name="com.google.ads.AdActivity"
android:configChanges="orientation|keyboard|keyboardHidden|screenLayout|uiMode|screenSize|smallestScreenSize" />
如果您仍然遇到错误,发布一些日志输出将非常有用。如果您使用Eclipse,您可以从Window->Show View->Other…->中找到logcat安卓->LogCat。或者,您可以使用adb logcat
和Android的adb工具从命令行获取logcat输出。