문제

It has to do with the Interstitial, I was getting the same problem with the Adview but solved it by adding the Adview to the xml and referencing in in java but you cannot do that with an Interstitial

public class MainActivity extends Activity {
    AdView adView;
    InterstitialAd interstitial;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.test);
        LinearLayout layout = (LinearLayout) findViewById(R.id.test);
        AdView adView = (AdView) this.findViewById(R.id.adView);

        AdRequest adRequest = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("TEST_DEVICE_ID").build();
        adView.loadAd(adRequest);

        InterstitialAd interstitial = new InterstitialAd(this);
        interstitial.setAdUnitId("banner");
        AdRequest adRequest2 = new AdRequest.Builder()
                .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
                .addTestDevice("TEST_DEVICE_ID").build();
        interstitial.loadAd(adRequest2);
        displayInterstitial();

    }

    public void displayInterstitial() {
        if (interstitial.isLoaded()) {
            interstitial.show();
        }
    }
}

LOGCAT:

02-21 19:48:29.390: E/AndroidRuntime(1929): FATAL EXCEPTION: main
02-21 19:48:29.390: E/AndroidRuntime(1929): Process: com.adtest, PID: 1929
02-21 19:48:29.390: E/AndroidRuntime(1929): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.adtest/com.adtest.MainActivity}: java.lang.NullPointerException
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2195)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread.access$800(ActivityThread.java:135)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.os.Handler.dispatchMessage(Handler.java:102)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.os.Looper.loop(Looper.java:136)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread.main(ActivityThread.java:5017)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at java.lang.reflect.Method.invokeNative(Native Method)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at java.lang.reflect.Method.invoke(Method.java:515)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at dalvik.system.NativeStart.main(Native Method)
02-21 19:48:29.390: E/AndroidRuntime(1929): Caused by: java.lang.NullPointerException
02-21 19:48:29.390: E/AndroidRuntime(1929):     at com.adtest.MainActivity.displayInterstitial(MainActivity.java:40)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at com.adtest.MainActivity.onCreate(MainActivity.java:35)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.Activity.performCreate(Activity.java:5231)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
02-21 19:48:29.390: E/AndroidRuntime(1929):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2159)
02-21 19:48:29.390: E/AndroidRuntime(1929):     ... 11 more
02-21 19:49:29.530: W/Ads(1929): Timed out waiting for ad response.
02-21 19:49:29.530: W/Ads(1929): Timed out waiting for ad response.
02-21 19:53:29.880: I/Process(1929): Sending signal. PID: 1929 SIG: 9
도움이 되었습니까?

해결책

You have created a local variable in your onCreate() and assigned the InterstitialAd instance to it:

InterstitialAd interstitial = new InterstitialAd(this);

This has nothing to do with the field in your class, thus when you call displayInterstitial() and reference the field in your class ... it's null

Also worth noting is that you're doing the same thing with AdView.

다른 팁

InterstitialAd interstitial = new InterstitialAd(this);

You are not writing your new instance to the class member, but you're creating a local variable instead. Remove the InterstitialAd at the beginning and you should be fine.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top