【问题标题】:Is It possible to access Interstitial Ads in Main Activity from other class?是否可以从其他类访问主活动中的插页式广告?
【发布时间】:2020-01-07 06:56:21
【问题描述】:

我想在单击按钮时显示插页式广告,但我为插页式广告构建了一个单独的类并在该类中创建了一个方法。现在,当我在初始屏幕中的单击按钮上输入时,我希望在初始屏幕上它访问 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();
    }
}

插页式类

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$1.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)

【问题讨论】:

    标签: java android ads interstitial


    【解决方案1】:

    错误是您的 Interstiatil 类 im Method prepareAd() 的第 26 行中的 NPE。 interstitialAd 为 null,因为它是在 onCreate() 中初始化的,该方法在创建活动时调用。 您应该将您的类更改为从 Activity 而不是从 Application 继承。 并且不要在您的 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");
            }
    }
    

    【讨论】:

    • 谢谢您,先生,它现在正在处理您的建议
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-08-14
    • 1970-01-01
    • 2013-04-04
    • 1970-01-01
    • 2021-08-19
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多