gpt4 book ai didi

java - Admob 间质性单例模式 java.lang.NullPointerException :

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

我正在使用Singleton模式来显示Admob Interstitial。其实模拟器上没问题。我在不同的智能手机上尝试过,没有问题。一切都运转良好。但当涉及到 Google Play 开发者控制台时,我遇到了这些错误。

java.lang.RuntimeException: 
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2200)
at android.app.ActivityThread.handleLaunchActivity (ActivityThread.java:2250)
at android.app.ActivityThread.access$800 (ActivityThread.java:139)
at android.app.ActivityThread$H.handleMessage (ActivityThread.java:1200)
at android.os.Handler.dispatchMessage (Handler.java:102)
at android.os.Looper.loop (Looper.java:136)
at android.app.ActivityThread.main (ActivityThread.java:5105)
at java.lang.reflect.Method.invokeNative (Native Method)
at java.lang.reflect.Method.invoke (Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run (ZygoteInit.java:792)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:608)
at dalvik.system.NativeStart.main (Native Method)
Caused by: java.lang.NullPointerException:
at x.y.z.ViewItemActivity.onCreate (ViewItemActivity.java:48)
at android.app.Activity.performCreate (Activity.java:5275)
at android.app.Instrumentation.callActivityOnCreate (Instrumentation.java:1087)
at android.app.ActivityThread.performLaunchActivity (ActivityThread.java:2164)

由:java.lang.NullPointerException引起: 在 x.y.z.ViewItemActivity.onCreate (ViewItemActivity.java:48)

ViewItemActivity 中的第 48 行:

if (ad.isLoaded()) {//Line 48
ad.show();
}

让我展示一下 AdManager 类和用法

AdManager 类:

package x;

import android.app.Activity;
import android.content.Context;
import android.content.res.Resources;
import android.util.Log;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.InterstitialAd;

import x.y.z.R;
import x.y.z.app.App;

public class AdManager {


static InterstitialAd ad;
private Context ctx;

public AdManager(Context ctx) {
this.ctx = ctx;
}

public void createAd() {
// Create an ad.
ad = new InterstitialAd(ctx);
ad.setAdUnitId(App.getResourses().getString(R.string.interstitial_ad_unit));

final AdRequest adRequest = new AdRequest.Builder().build();

// Load the interstitial ad.
ad.loadAd(adRequest);

// Log.d("admobshowing","asdf");
}

public InterstitialAd getAd() {
return ad;
}
}

我在OnCreate中的MainActivity中请求:

AdManager adManager = new AdManager(MainActivity.this);
adManager.createAd();

我在另一个 Activity - ViewItemActivity - setContentView 之前显示它:

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AdManager adManager = new AdManager(ViewItemActivity.this);
InterstitialAd ad = adManager.getAd();
if (ad.isLoaded()) {
ad.show();
}

setContentView(R.layout.activity_view_item);

....

....

当用户按后退按钮时再次请求:- ViewItemActivity

@Override
public void onBackPressed(){
AdManager adManager = new AdManager(ViewItemActivity.this);
adManager.createAd();
super.onBackPressed();
}

因此,当我单独尝试时,我在模拟器或真实智能手机中不会出现错误,但在 Google Play 开发者控制台中,我会遇到数十个空指针异常。我该如何解决我的问题?

最佳答案

我认为单例模式不适合您的用例。您正在尝试为每个上下文创建一个新广告,因此在 AdManager 实例中保留该广告没有任何好处。您可以将 AdManager 更改为以下内容,并删除对 createAd 的所有调用。

public class AdManager {

private InterstitialAd ad;
private Context ctx;

public AdManager(Context ctx) {
this.ctx = ctx;
}

private void createAd() {
// Create an ad.
ad = new InterstitialAd(ctx);
ad.setAdUnitId(App.getResourses().getString(R.string.interstitial_ad_unit));

final AdRequest adRequest = new AdRequest.Builder().build();

// Load the interstitial ad.
ad.loadAd(adRequest);
}

public InterstitialAd getAd() {
if (ad == null) {
createAd();
}
return ad;
}
}

关于java - Admob 间质性单例模式 java.lang.NullPointerException :,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58243374/

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