是否可以从其他 class 访问 Main Activity 中的插页式广告?

Is It possible to access Interstitial Ads in Main Activity from other class?

我想在点击按钮时显示插页式广告,但我为插页式广告构建了一个单独的 class,并在此 class 中制作了一个方法。现在我希望在启动画面上,当我在启动画面中单击按钮时,它会访问 Interstitial Class 的方法并显示广告,但是当我单击按钮时,它会在 Interstitial Class 中给我错误.

启动画面

package com.deitel.adsmobapp;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.NativeExpressAdView;

public class SplashScreen extends AppCompatActivity {
    private AdView mAdview;
    Button btn_move;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_splash_screen);
        btn_move=findViewById(R.id.btn_move);
        btn_move.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Interstitial interstitial=new Interstitial();
                interstitial.prepareAd();
                Intent i=new Intent(SplashScreen.this,MainActivity.class);
                startActivity(i);
            }
        });
        /*Banner Ads*/
        MobileAds.initialize(this,"ca-app-pub-3940256099942544~3347511713");
        mAdview=findViewById(R.id.adView);
        AdRequest adRequest=new AdRequest.Builder().build();
        mAdview.loadAd(adRequest);

    }

    @Override
    protected void onPause() {
        super.onPause();
        finish();
    }
}

插页式 class

package com.deitel.adsmobapp;

import android.app.Activity;
import android.app.Application;
import android.util.Log;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

public class Interstitial extends Application {
    private InterstitialAd interstitialAd;

    @Override
    public void onCreate() {
        super.onCreate();

        SplashScreen splashScreen=new SplashScreen();
        interstitialAd = new InterstitialAd(splashScreen);
        interstitialAd.setAdUnitId("ca-app-pub-3940256099942544/1033173712");
        AdRequest adRequest=new AdRequest.Builder().build();
        interstitialAd.loadAd(adRequest);
        prepareAd();
    }
    public void prepareAd() {

        if (interstitialAd.isLoaded())
        {
            interstitialAd.show();
        }
        else
            {
                Log.d("tag","Ads is not loaded");
            }

    }
}

错误

01-06 17:05:51.907 7044-7044/com.deitel.adsmobapp E/AndroidRuntime: FATAL EXCEPTION: main
    java.lang.NullPointerException
        at com.deitel.adsmobapp.Interstitial.prepareAd(Interstitial.java:26)
        at com.deitel.adsmobapp.SplashScreen.onClick(SplashScreen.java:31)
        at android.view.View.performClick(View.java:4084)
        at android.view.View$PerformClick.run(View.java:16966)

        at android.os.Handler.dispatchMessage(Handler.java:92)

错误是您的 Interstiatil Class im 方法 prepareAd() 的第 26 行中的 NPE。 interstitialAd 为 null,因为它是在创建 activity 时调用的 onCreate() 中初始化的。 您应该将 class 更改为继承自 Activity 而不是应用程序。 并且不要在您的 onClick 方法中调用 prepareAd()。

public class Interstitial extends Activity

在您的 prepareAd() 方法中检查 null 可能是明智的做法:

public void prepareAd() {
    if (interstitialAd != null && interstitialAd.isLoaded())
    {
        interstitialAd.show();
    }
    else
        {
            Log.d("tag","Ads is not loaded");
        }
}