0

アプリケーションの次のコードで nullpointer 例外が発生します。

@Override
protected void onResume() {
    super.onResume();    //To change body of overridden methods use File | Settings | File Templates.

    // Create the adView
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);

    LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);

    // Add the adView to it
    layout.addView(adView); //NullPointerException here

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());


    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isButtonLabelsEnabled = sp.getBoolean("isButtonLabelsEnabled", false);
    if (!isButtonLabelsEnabled) {
        stripLabels();
    } else {
        setContentView(R.layout.languageselection);
    }
}

この問題を再現できません。エミュレーター、Galaxy S2 デバイス、ZTE ブレードの両方でアプリのすべての機能を試してみましたが、問題は見つかりませんでした。

多くのユーザーがこれを報告していますが、彼らはその理由を示していません。ただ、開こうとすると強制的に閉じられるだけです。

おそらく私はアンドロイドのライフサイクルを正しく使用していませんか?

回避策として、通過できるように AdMob のものを try/catch でラップしました。

何か案は?

4

2 に答える 2

1

onResume メソッドではなく、onCreate メソッドで LinearLayout オブジェクトを取得してみてください。これがうまくいくことを願っています。

于 2012-01-02T05:52:32.947 に答える
0

このようにしてみてください:

private LinearLayout layout;

onCreate(){

       layout = (LinearLayout) findViewById(R.id.ad_layout);

}


@Override
protected void onResume() {
    super.onResume();    //To change body of overridden methods use File | Settings | File Templates.

    // Create the adView
    adView = new AdView(this, AdSize.BANNER, MY_AD_UNIT_ID);

    //LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);

    // Add the adView to it
    layout.addView(adView); //NullPointerException here

    // Initiate a generic request to load it with an ad
    adView.loadAd(new AdRequest());


    SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this);
    boolean isButtonLabelsEnabled = sp.getBoolean("isButtonLabelsEnabled", false);
    if (!isButtonLabelsEnabled) {
        stripLabels();
    } else {
        setContentView(R.layout.languageselection);
    }
}
于 2012-01-01T17:58:32.307 に答える