有没有办法解决“尝试调用虚拟方法”的问题?

时间:2019-02-12 14:57:10

标签: android nullpointerexception apk adview

我创建了我的应用,现在我正尝试添加AdMob广告,但是我陷入了即时崩溃和问题,并且不知道如何解决。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.glitchrun.sapphire/com.glitchrun.sapphire.PTPlayer}: java.lang.NullPointerException: Attempt to invoke virtual method void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest) on a null object reference
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2957)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3032)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1696)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6944)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:327)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1374)
 Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)' on a null object reference
    at com.glitchrun.sapphire.PTPlayer.onCreate(PTPlayer.java:68)
    at android.app.Activity.performCreate(Activity.java:7183)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1220)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2910)

我尝试删除import com.google.android.gms.ads.AdView;,但此后广告没有在应用中展示。但是,即使广告没有展示,我在AdMob中仍然有要求和匹配率。如何解决问题?

import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdView;

AdView mAdView = findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);`

1 个答案:

答案 0 :(得分:0)

尝试正确读取异常。这是一个空指针异常。

  

java.lang.NullPointerException:尝试在空对象引用上调用虚方法void com.google.android.gms.ads.AdView.loadAd(com.google.android.gms.ads.AdRequest)

它说,对空对象调用了loadAd方法。这意味着您的AdView对象为null。可能是未正确初始化或ID错误。