我正在尝试在我的应用上显示广告条幅,而且我似乎遇到了问题。无论何时,当我尝试加载广告时,应用程序都会崩溃并提及相关信息,因此它是空引用。尽管如此,我并不乐观。在应用上加载AdMob广告的问题
下面是AD浏览的XML:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/gameLayout">
<com.google.android.gms.ads.AdView
android:id="@+id/bannerAd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
ads:adSize="BANNER"
ads:adUnitId="MY_ADUNITID" />
</FrameLayout>
这里是Java代码使用广告:
//load ads
AdView adView = (AdView)findViewById(R.id.bannerAd);
AdRequest.Builder request = new AdRequest.Builder();
request.addTestDevice(AdRequest.DEVICE_ID_EMULATOR);
adView.loadAd(request.build());
这里是logcat的:
09-21 21:55:39.300: E/AndroidRuntime(1168): FATAL EXCEPTION: main
09-21 21:55:39.300: E/AndroidRuntime(1168): Process: com.project.llb, PID: 1168
09-21 21:55:39.300: E/AndroidRuntime(1168): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.twentytwentythree.sab/com.twentytwentythree.sab.runGraphics}: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2596)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2653)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread.access$800(ActivityThread.java:156)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.os.Handler.dispatchMessage(Handler.java:102)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.os.Looper.loop(Looper.java:157)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread.main(ActivityThread.java:5872)
09-21 21:55:39.300: E/AndroidRuntime(1168): at java.lang.reflect.Method.invoke(Native Method)
09-21 21:55:39.300: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:852)
09-21 21:55:39.300: E/AndroidRuntime(1168): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:668)
09-21 21:55:39.300: E/AndroidRuntime(1168): Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
09-21 21:55:39.300: E/AndroidRuntime(1168): at com.twentytwentythree.sab.runGraphics.onCreate(runGraphics.java:94)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.Activity.performCreate(Activity.java:5312)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1111)
09-21 21:55:39.300: E/AndroidRuntime(1168): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2552)
09-21 21:55:39.300: E/AndroidRuntime(1168): ... 9 more
这里来自androidmanifest的代码:
<meta-data android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
<activity
android:name="com.google.android.gms.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation" >
</activity>
现在我使用addTestDevice方法,因为我认为这是我需要使用的,因为我的应用程序还没有在市场上。我确实有我的admob帐户设置,我有这样的横幅设置。
如果您有任何想法,请让我知道。非常感谢。
请发布完整的xml文件和logcat输出。 – 2014-09-22 02:51:12
@JuniorBuckeridge我加了你的要求。对于那个很抱歉。 – sboehnke 2014-09-22 02:58:13
您确定您在可以使用Google Play服务的设备上运行应用吗? – 2014-09-22 03:15:15