gpt4 book ai didi

java - Android 调用广告类

转载 作者:行者123 更新时间:2023-12-02 02:53:16 27 4
gpt4 key购买 nike

我正在尝试在我的 Android 游戏中实现广告。我创建了一个名为 Ads 的类,并使用以下方法从 switch case 内的另一个类调用它。

 public class ControlCenter1 extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_control_center1);

public ControlCenter1(Context context)
{
mScene = ParrotsConstent.E_SCENARIO.MENU;
mContext = context;
mScore = new Score();
mSound = new Sound(context);
mTimer = new Timer(ParrotsConstent.MAX_TIME);
mAnimalPic = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mPicBak = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mEffect = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mDisappearToken = new int[(int) ParrotsConstent.GRID_NUM][(int) ParrotsConstent.GRID_NUM];
mToken = new ActionTokenPool();
init();
}

public static Handler mHandler = new Handler(){
@Override
public void handleMessage(Message msg)
{
switch(msg.what)
{
case GAME_OVER_START:
{
CtlTip2 ctl = (CtlTip2) drawTip2.control;
ctl.init(ParrotsConstent.E_TIP.GAMEOVER.ordinal());
mSound.play(ParrotsConstent.E_SOUND.TIMEOVER);
break;
}

case GAME_OVER_END:
{
Ads ads = new Ads();
ads.Adss();
break;
}
}
}

带有 Google 广告的类如下:

public class Ads extends AppCompatActivity {

InterstitialAd mInterstitialAd;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ads);
}

public void Adss()
{
AdRequest adRequest = new AdRequest.Builder().build();


mInterstitialAd = new InterstitialAd(this);
mInterstitialAd.setAdUnitId(getString(R.string.admob_interstitial_id));

mInterstitialAd.loadAd(adRequest);

mInterstitialAd.setAdListener(new AdListener() {
public void onAdLoaded() {
displayInterstitial();
onPause();
}

public void onAdClosed() {
onResume();
mScene = ParrotsConstent.E_SCENARIO.RESULT;
}
});
}

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

没有广告,游戏运行流畅。在运行游戏之前我没有收到错误,但是一旦我运行游戏并且游戏结束,游戏就会停止并出现以下 fatal error 。

致命异常:主要 进程:PID:2566

java.lang.NullPointerException: Attempt to invoke virtual method android.content.res.Resources android.content.Context.getResources()' on a null object reference
at android.content.ContextWrapper.getResources(ContextWrapper.java:86)
at android.view.ContextThemeWrapper.getResourcesInternal(ContextThemeWrapper.java:127) at android.view.ContextThemeWrapper.getResources(ContextThemeWrapper.java:121) atandroid.support.v7.app.AppCompatActivity.getResources(AppCompatActivity.java:549)
at android.content.Context.getString(Context.java:476)
at com.gamingtechnology.parrots.Ads.Adss(Ads.java:69)
at com.gamingtechnology.parrots.Core.ControlCenter1$2.handleMessage(ControlCenter1.java:1188)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)

最佳答案

您无法实例化这样的 Activity。它根本行不通。

你必须通过startActivity来启动它。

关于java - Android 调用广告类,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43480688/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com