无法显示插页式广告
Unable to show Interstitial ad
我已经看完了这个问题之前的所有答案,但是我的错误和方法与之前的问题略有不同。
我的应用程序中只有三个视图:登录、注册和主 .每次从一个视图 转换 到另一个视图时,我都想展示一个插页式广告。
为了完成这个,我正在初始化、加载和显示每个视图开头的广告:
MainActivity.java
package com.brain.passive;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.*;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
public EditText emailId, passwd;
Button btnSignUp;
TextView signIn;
FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-7147930999992278/2850010809", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i("Loaded", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i("FailedError", loadAdError.getMessage());
mInterstitialAd = null;
}
});
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
\Rest of the logic for App.
}
}
LoginActivity.java 和 SignupActivity.java 遵循相同的格式(先初始化并显示广告,然后再显示其余内容的逻辑)。
问题是广告没有显示。以下是错误日志:
E/cr_VariationsUtils: Failed reading seed file "/data/user/0/com.brain.passive/app_webview/variations_seed": /data/user/0/com.brain.passive/app_webview/variations_seed (No such file or directory)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brain.passive/com.brain.passive.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void c.b.b.a.a.x.a.b(c.b.b.a.a.k)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void c.b.b.a.a.x.a.b(c.b.b.a.a.k)' on a null object reference
at com.brain.passive.MainActivity.onCreate(:2)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
在详细信息中还显示了这些行:
W/GooglePlayServicesUtil: Google Play services out of date for com.brainiac.passiveincome. Requires 212800000 but found 202414022
W/GoogleApiManager: The service for c.b.b.a.h.c.c is not available: b{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
W/VideoCapabilities: Unrecognized profile 4 for video/hevc
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_MediaCodecUtil: HW encoder for video/avc is not available on this device.
我无法确定这是视频不兼容问题还是我在 google 广告设置中犯了一些错误。请帮助。
在重写函数时,调用 super 是必要的。这就是导致内部函数出现空指针异常的原因。
InterstitialAd.load(this, "ca-app-pub-7147930999992246/2850110809", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
Log.i("Loaded", "onAdLoaded");
interstitialAd.show(LoginActivity.this);
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
super.onAdFailedToShowFullScreenContent(adError);
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
super.onAdDismissedFullScreenContent();
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
super.onAdShowedFullScreenContent();
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i("FailedError", loadAdError.getMessage());
}
});
我已经看完了这个问题之前的所有答案,但是我的错误和方法与之前的问题略有不同。
我的应用程序中只有三个视图:登录、注册和主 .每次从一个视图 转换 到另一个视图时,我都想展示一个插页式广告。
为了完成这个,我正在初始化、加载和显示每个视图开头的广告:
MainActivity.java
package com.brain.passive;
import android.content.Intent;
import android.util.Log;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.gms.ads.*;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.auth.AuthResult;
import com.google.firebase.auth.FirebaseAuth;
import com.google.firebase.auth.FirebaseUser;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;
import com.google.android.gms.ads.interstitial.InterstitialAd;
import com.google.android.gms.ads.interstitial.InterstitialAdLoadCallback;
public class MainActivity extends AppCompatActivity {
public EditText emailId, passwd;
Button btnSignUp;
TextView signIn;
FirebaseAuth firebaseAuth;
private FirebaseAuth.AuthStateListener authStateListener;
private InterstitialAd mInterstitialAd;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {
}
});
AdRequest adRequest = new AdRequest.Builder().build();
InterstitialAd.load(this,"ca-app-pub-7147930999992278/2850010809", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
mInterstitialAd = interstitialAd;
Log.i("Loaded", "onAdLoaded");
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i("FailedError", loadAdError.getMessage());
mInterstitialAd = null;
}
});
mInterstitialAd.setFullScreenContentCallback(new FullScreenContentCallback(){
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
mInterstitialAd = null;
Log.d("TAG", "The ad was shown.");
}
});
if (mInterstitialAd != null) {
mInterstitialAd.show(MainActivity.this);
} else {
Log.d("TAG", "The interstitial ad wasn't ready yet.");
}
\Rest of the logic for App.
}
}
LoginActivity.java 和 SignupActivity.java 遵循相同的格式(先初始化并显示广告,然后再显示其余内容的逻辑)。
问题是广告没有显示。以下是错误日志:
E/cr_VariationsUtils: Failed reading seed file "/data/user/0/com.brain.passive/app_webview/variations_seed": /data/user/0/com.brain.passive/app_webview/variations_seed (No such file or directory)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.brain.passive/com.brain.passive.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'void c.b.b.a.a.x.a.b(c.b.b.a.a.k)' on a null object reference
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2913)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void c.b.b.a.a.x.a.b(c.b.b.a.a.k)' on a null object reference
at com.brain.passive.MainActivity.onCreate(:2)
at android.app.Activity.performCreate(Activity.java:7136)
at android.app.Activity.performCreate(Activity.java:7127)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1271)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2893)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3048)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:78)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:108)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:68)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1808)
at android.os.Handler.dispatchMessage(Handler.java:106)
at android.os.Looper.loop(Looper.java:193)
at android.app.ActivityThread.main(ActivityThread.java:6669)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:493)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:858)
在详细信息中还显示了这些行:
W/GooglePlayServicesUtil: Google Play services out of date for com.brainiac.passiveincome. Requires 212800000 but found 202414022
W/GoogleApiManager: The service for c.b.b.a.h.c.c is not available: b{statusCode=SERVICE_VERSION_UPDATE_REQUIRED, resolution=null, message=null}
W/VideoCapabilities: Unrecognized profile 4 for video/hevc
I/VideoCapabilities: Unsupported profile 4 for video/mp4v-es
W/cr_MediaCodecUtil: HW encoder for video/avc is not available on this device.
我无法确定这是视频不兼容问题还是我在 google 广告设置中犯了一些错误。请帮助。
在重写函数时,调用 super 是必要的。这就是导致内部函数出现空指针异常的原因。
InterstitialAd.load(this, "ca-app-pub-7147930999992246/2850110809", adRequest,
new InterstitialAdLoadCallback() {
@Override
public void onAdLoaded(@NonNull InterstitialAd interstitialAd) {
// The mInterstitialAd reference will be null until
// an ad is loaded.
Log.i("Loaded", "onAdLoaded");
interstitialAd.show(LoginActivity.this);
interstitialAd.setFullScreenContentCallback(new FullScreenContentCallback() {
@Override
public void onAdFailedToShowFullScreenContent(AdError adError) {
// Called when fullscreen content failed to show.
super.onAdFailedToShowFullScreenContent(adError);
Log.d("TAG", "The ad failed to show.");
}
@Override
public void onAdDismissedFullScreenContent() {
// Called when fullscreen content is dismissed.
super.onAdDismissedFullScreenContent();
Log.d("TAG", "The ad was dismissed.");
}
@Override
public void onAdShowedFullScreenContent() {
// Called when fullscreen content is shown.
// Make sure to set your reference to null so you don't
// show it a second time.
super.onAdShowedFullScreenContent();
Log.d("TAG", "The ad was shown.");
}
});
}
@Override
public void onAdFailedToLoad(@NonNull LoadAdError loadAdError) {
// Handle the error
Log.i("FailedError", loadAdError.getMessage());
}
});