- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用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/
我最近购买了《C 编程语言》并尝试了 Ex 1-8这是代码 #include #include #include /* * */ int main() { int nl,nt,nb;
早上好!我有一个变量“var”,可能为 0。我检查该变量是否为空,如果不是,我将该变量保存在 php session 中,然后调用另一个页面。在这个新页面中,我检查我创建的 session 是否为空,
我正在努力完成 Learn Python the Hard Way ex.25,但我无法理解某些事情。这是脚本: def break_words(stuff): """this functio
我是一名优秀的程序员,十分优秀!