我终于将广告投放到我的应用中了(我认为)。但我在1号线遇到了崩溃。见代码:
public class ... extends ListActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
String[] rmenu = getResources().getStringArray(R.array.root_menu);
if (rmenu != null) {
setListAdapter(new ArrayAdapter<String>(this, R.layout.list_item, rmenu));
}
setContentView(R.layout.main);
//
// Admob Code
// Create the adView
AdView av = new AdView(this, AdSize.BANNER, "publisher_id");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="@+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.mainLayout);
// Add the adView to it
layout.addView(av);
// Initiate a generic request to load it with an ad
av.loadAd(new AdRequest());
}
崩溃发生在以下行:layout.addView(av);
我在上面引用的布局文件中添加了一个adview(mainLayout)。 我将在下面添加xml代码:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/mainLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#000000" >
<adView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:text="Loading..." />
</LinearLayout>
任何帮助将不胜感激!或者只是指向正确的方向。
Logcat文件::
'code'04-22 18:00:29.862: DEBUG/AndroidRuntime(307): >>>>>>>>>>>>>> AndroidRuntime START <<<<<<<<<<<<<<
04-22 18:00:29.862: DEBUG/AndroidRuntime(307): CheckJNI is ON
04-22 18:00:30.002: DEBUG/AndroidRuntime(307): --- registering native functions ---
04-22 18:00:30.642: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.xxxxx/.xxxxx }
04-22 18:00:30.762: DEBUG/AndroidRuntime(307): Shutting down VM
04-22 18:00:30.772: DEBUG/dalvikvm(307): Debugger has detached; object registry had 1 entries
04-22 18:00:30.852: INFO/ActivityManager(59): Start proc com.xxxxx for activity com.xxxxx/.xxxxx: pid=314 uid=10036 gids={3003}
04-22 18:00:30.872: INFO/AndroidRuntime(307): NOTE: attach of thread 'Binder Thread #3' failed
04-22 18:00:31.812: DEBUG/AndroidRuntime(314): Shutting down VM
04-22 18:00:31.812: WARN/dalvikvm(314): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): FATAL EXCEPTION: main
04-22 18:00:31.842: ERROR/AndroidRuntime(314): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.xxxxx/com.xxxxx.xxxxx}: java.lang.NullPointerException
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.os.Handler.dispatchMessage(Handler.java:99)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.os.Looper.loop(Looper.java:123)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.main(ActivityThread.java:4627)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at java.lang.reflect.Method.invokeNative(Native Method)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at java.lang.reflect.Method.invoke(Method.java:521)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at dalvik.system.NativeStart.main(Native Method)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): Caused by: java.lang.NullPointerException
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at com.xxxxx.xxxxxx.onCreate(txchl.java:36)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-22 18:00:31.842: ERROR/AndroidRuntime(314): ... 11 more
===========更新=============================
固定。
好的,我不会再遇到崩溃了,大约几分钟后我就看到广告了!
这就是我所做的。在http://code.google.com/mobile/ads/docs/android/fundamentals.html本教程您将看到第一步是将SDK JAR添加到您的项目中。我最初使用的是较新的JAR文件,名为“googleadmobadssdkandroid”。我仍然有较旧的下载,标题为“admob-sdk-android_20101109”,我将BOTH JAR文件添加到我的项目中,上面的代码现在可以正常工作。
答案 0 :(得分:1)
您的LinearLayout似乎缺少以下第二行:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res/your.package.name"
然后,您是否已将Jar添加到构建路径并且它位于“libs”文件夹中?
最后,我有时会忘记在res / values
中创建attrs.xml文件答案 1 :(得分:1)
固定。
好的,我不会再遇到崩溃了,大约几分钟后我就看到广告了!
这就是我所做的。在http://code.google.com/mobile/ads/docs/android/fundamentals.htmlthis教程您将看到第一步是将SDK JAR添加到您的项目中。我最初使用的是较新的JAR文件,名为“googleadmobadssdkandroid”。我仍然有较旧的下载,标题为“admob-sdk-android_20101109”,我将BOTH JAR文件添加到我的项目中,上面的代码现在可以正常工作。