显示admob广告时的Android NullPoinerException

时间:2013-11-26 01:08:02

标签: android nullpointerexception admob adview

我的应用中有以下代码:

AdView ad;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ad = new AdView(this);
    ad.setAdSize(AdSize.SMART_BANNER);
    ad.setAdUnitId("left out from question");
    RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
    layout.addView(ad);
    AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
    ad.loadAd(adRequestBuilder.build());
    setContentView(R.layout.activity_main);

}

我一直在我将adView添加到线性布局的行上得到一个空指针异常。这可能是什么问题?我的LogCat发布在下面:

11-25 19:55:46.035: E/AndroidRuntime(346): FATAL EXCEPTION: main
11-25 19:55:46.035: E/AndroidRuntime(346): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.adtest/com.example.adtest.MainActivity}: java.lang.NullPointerException
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.os.Handler.dispatchMessage(Handler.java:99)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.os.Looper.loop(Looper.java:123)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread.main(ActivityThread.java:4627)
11-25 19:55:46.035: E/AndroidRuntime(346):  at java.lang.reflect.Method.invokeNative(Native Method)
11-25 19:55:46.035: E/AndroidRuntime(346):  at java.lang.reflect.Method.invoke(Method.java:521)
11-25 19:55:46.035: E/AndroidRuntime(346):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
11-25 19:55:46.035: E/AndroidRuntime(346):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
11-25 19:55:46.035: E/AndroidRuntime(346):  at dalvik.system.NativeStart.main(Native Method)
11-25 19:55:46.035: E/AndroidRuntime(346): Caused by: java.lang.NullPointerException
11-25 19:55:46.035: E/AndroidRuntime(346):  at com.example.adtest.MainActivity.onCreate(MainActivity.java:21)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
11-25 19:55:46.035: E/AndroidRuntime(346):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
11-25 19:55:46.035: E/AndroidRuntime(346):  ... 11 more

1 个答案:

答案 0 :(得分:1)

尝试替换

RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
layout.addView(ad);
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
ad.loadAd(adRequestBuilder.build());
setContentView(R.layout.activity_main);

setContentView(R.layout.activity_main);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.layout);
layout.addView(ad);
AdRequest.Builder adRequestBuilder = new AdRequest.Builder();
ad.loadAd(adRequestBuilder.build());

你试图在setContentView之前访问R.id.layout,这将导致nullpointerexception