添加admod Java代码后Android应用程序崩溃

时间:2020-04-16 13:13:31

标签: java android crash admob

班级:

public class MainActivity extends AppCompatActivity {

    private AdView mAdView;
    TextView percents;
    Singleton m_Inst = Singleton.getInstance();


    private int volume = 50;
    private int boost = 0;


    private AudioManager audio;

    public MainActivity() {

    }

    @TargetApi(Build.VERSION_CODES.JELLY_BEAN)
    @SuppressLint("WrongViewCast")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        AdView adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

        MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");

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

LOGCAT

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.bassbooster.android_simple_eq.MainActivity.onCreate(MainActivity.java:62)*```

1 个答案:

答案 0 :(得分:0)

mAdView为空。

这可能是因为您没有为布局充气。

尝试对包含首先包含ID的adView的布局进行充气。

您可以使用setContentView(int)

在活动的onCreate内执行此操作

编辑:

按照评论中的要求,我正在修改您的代码:

MainActivity.java:

public class MainActivity extends AppCompatActivity {

private AdView mAdView;
TextView percents;
Singleton m_Inst = Singleton.getInstance();


private int volume = 50;
private int boost = 0;


private AudioManager audio;

public MainActivity() {

}

@TargetApi(Build.VERSION_CODES.JELLY_BEAN)
@SuppressLint("WrongViewCast")
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //first change
    setContentView(R.layout.main_activity)


    AdView adView = new AdView(this);
    adView.setAdSize(AdSize.BANNER);
    adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111");

    MobileAds.initialize(this, "ca-app-pub-3940256099942544/6300978111");


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

main_activity.xml:

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
   xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent">

   <com.google.android.gms.ads.AdView
     android:id="@+id/adView"
     android:layout_width="wrap_content"
     android:layout_height="wrap_content"
     app:layout_constraintEnd_toEndOf="parent"
     app:layout_constraintStart_toStartOf="parent"
     app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

如您所见,findViewById指向main_activity.xml中AdView的ID。因此,您不会调用空对象引用。 这将是一个基本设置,应该可以正常工作。

如果您还有其他问题,请告诉我。