Admob横幅失败了

时间:2014-05-11 09:52:08

标签: admob

我正在尝试在我的应用程序中添加横幅,但我有这个错误。

logcat的:

05-11 09:41:23.559: E/AndroidRuntime(1384): FATAL EXCEPTION: main
05-11 09:41:23.559: E/AndroidRuntime(1384): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.mysteryofcontrasts/com.example.mysteryofcontrasts.MysteryOfContrasts}: java.lang.NullPointerException
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2059)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread.access$600(ActivityThread.java:130)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1195)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.os.Handler.dispatchMessage(Handler.java:99)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.os.Looper.loop(Looper.java:137)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread.main(ActivityThread.java:4745)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at java.lang.reflect.Method.invokeNative(Native Method)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at java.lang.reflect.Method.invoke(Method.java:511)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at dalvik.system.NativeStart.main(Native Method)
05-11 09:41:23.559: E/AndroidRuntime(1384): Caused by: java.lang.NullPointerException
05-11 09:41:23.559: E/AndroidRuntime(1384):     at com.example.mysteryofcontrasts.MysteryOfContrasts.onCreate(MysteryOfContrasts.java:223)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.Activity.performCreate(Activity.java:5008)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079)
05-11 09:41:23.559: E/AndroidRuntime(1384):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023)
05-11 09:41:23.559: E/AndroidRuntime(1384):     ... 11 more

MainClass:

private AdView adview;
@Override
 public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);

 LinearLayout layout=(LinearLayout)findViewById(R.id.reklam);
 adview = new AdView(this, AdSize.SMART_BANNER,
         "ca-app-pub-123456789132/123456");

 layout.addView(adview);
 adview.loadAd(new AdRequest());



}

1 个答案:

答案 0 :(得分:0)

Caused by: java.lang.NullPointerException 05-11 09:41:23.559: 
at com.example.mysteryofcontrasts.MysteryOfContrasts.onCreate(MysteryOfContrasts.java:223)

查看#onCreate的第223行。当它不应该是的东西是空的。 我愿意打赌它是

layout.addView(adview);

因为看起来你事先没有调用 setContentView(resouceIdOfYourActivity);