gpt4 book ai didi

java - 将 Admob 添加到 Android 应用程序 : AdView

转载 作者:行者123 更新时间:2023-11-30 09:49:29 25 4
gpt4 key购买 nike

我距离最终将广告植入我的应用程序只有一步之遥(我认为)。但我在 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);

我在上面引用的布局文件 (mainLayout) 中添加了一个 adview。我将在下面添加该 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”,我将两个 JAR 文件添加到我的项目中,上面的代码现在可以运行了。

最佳答案

您的 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 文件

关于java - 将 Admob 添加到 Android 应用程序 : AdView,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/5758237/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com