Adview错误,一段时间后崩溃

时间:2014-04-10 00:17:24

标签: android crash adview

我的代码中的错误在哪里我的adview设置有问题。 如果我不使用AdListener,一切运行良好。 只有一段时间后我的应用程序崩溃但我不知道出了什么问题?

// Create the adView.
adView = new AdView(this);
adView.setAdUnitId("*****");
adView.setAdSize(AdSize.BANNER);

// Initiate a generic request.
AdRequest adRequest = new AdRequest.Builder().build();
final LinearLayout layout = (LinearLayout) findViewById(R.id.mainLayout);
// Load the adView with the ad request.
adView.setBackgroundColor(Color.BLACK);
adView.loadAd(adRequest);
adView.setAdListener(new AdListener() {
    public void onAdLoaded() {
        layout.addView(adView);
        adView = new AdView(getApplication());
    }
});

logcat的:

04-10 02:05:45.888  31381-31381/**************** E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: ****************, PID: 31381
java.lang.RuntimeException: Unable to start activity ComponentInfo{****************/****************.MainActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
        at android.app.ActivityThread.access$900(ActivityThread.java:161)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:157)
        at android.app.ActivityThread.main(ActivityThread.java:5356)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at ****************.MainActivity.onCreate(MainActivity.java:145)
        at android.app.Activity.performCreate(Activity.java:5426)
        at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
            at android.app.ActivityThread.access$900(ActivityThread.java:161)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:157)
            at android.app.ActivityThread.main(ActivityThread.java:5356)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
            at dalvik.system.NativeStart.main(Native Method)

1 个答案:

答案 0 :(得分:0)

如果你在onClick监听器中得到一个空指针,我假设当你尝试layoutfindViewBy被设置为null。检查以确保xml中该视图的id实际为mainLayout