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

时间:2018-06-24 09:39:59

标签: android nullpointerexception

我一直在尝试在其中一项活动中展示adbob横幅广告,但我一直收到这个奇怪的错误,我尝试做不同的事情,但没有一个帮助解决问题,我被困在这上,请帮助我解决此错误, 谢谢。 这是logcat

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.rewardoapps.network/com.rewardoapps.network.ChatActivity}: 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:2817)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892)
    at android.app.ActivityThread.-wrap11(Unknown Source:0)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593)
    at android.os.Handler.dispatchMessage(Handler.java:105)
    at android.os.Looper.loop(Looper.java:164)
    at android.app.ActivityThread.main(ActivityThread.java:6541)
    at java.lang.reflect.Method.invoke(Native Method)
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)
 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.rewardoapps.network.ChatActivity.onCreate(ChatActivity.java:44)
    at android.app.Activity.performCreate(Activity.java:6975)
    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1213)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2770)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2892) 
    at android.app.ActivityThread.-wrap11(Unknown Source:0) 
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1593) 
    at android.os.Handler.dispatchMessage(Handler.java:105) 
    at android.os.Looper.loop(Looper.java:164) 
    at android.app.ActivityThread.main(ActivityThread.java:6541) 
    at java.lang.reflect.Method.invoke(Native Method) 
    at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240) 
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767) 

这是我的ChatActivity.java活动

public class ChatActivity extends ActivityBase implements MsgImageChooseDialog.AlertPositiveListener {
private AdView AdView;

Toolbar mToolbar;

Fragment fragment;
Boolean restore = false;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_dialogs);

    mToolbar = (Toolbar) findViewById(R.id.toolbar);

    setSupportActionBar(mToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    MobileAds.initialize(this,
            "ca-app-pub-3940256099942544~3347511713");

    AdRequest adRequestBanner = new AdRequest.Builder().build();
    AdView = (AdView)this.findViewById(R.id.adView);
    AdView.loadAd(adRequestBanner);

    if (savedInstanceState != null) {

        fragment = getSupportFragmentManager().getFragment(savedInstanceState, "currentFragment");

        restore = savedInstanceState.getBoolean("restore");

    } else {

        fragment = new ChatFragment();

        restore = false;
    }

    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.container_body, fragment)
            .commit();
}

.xml布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
xmlns:ads="http://schemas.android.com/apk/res-auto"

android:layout_height="match_parent" android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:paddingBottom="@dimen/activity_vertical_margin"
android:orientation="vertical"
tools:context="com.rewardoapps.network.ChatActivity" >

    <LinearLayout            android:id="@+id/container_toolbar"            android:layout_width="match_parent"            android:layout_height="wrap_content"            android:orientation="vertical">            <include                android:id="@+id/toolbar"                layout="@layout/toolbar" />        </LinearLayout>
<LinearLayout
    android:id="@+id/container_a"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:background="#00ffffff"
    android:gravity="bottom|center_vertical|center_horizontal"
    android:orientation="vertical"
    android:visibility="visible">

    <com.google.android.gms.ads.AdView
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"></com.google.android.gms.ads.AdView>
</LinearLayout>
<FrameLayout
    android:id="@+id/container_body"
    android:layout_width="fill_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:visibility="visible" />

这是ActivityBase.java

public class ActivityBase extends AppCompatActivity implements Constants {

public static final String TAG = "ActivityBase";

private ProgressDialog pDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    initpDialog();
}

protected void initpDialog() {

    pDialog = new ProgressDialog(this);
    pDialog.setMessage(getString(R.string.msg_loading));
    pDialog.setCancelable(false);
}

protected void showpDialog() {

    if (!pDialog.isShowing()) {

        try {

            pDialog.show();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

protected void hidepDialog() {

    if (pDialog.isShowing()) {

        try {

            pDialog.dismiss();

        } catch (Exception e) {

            e.printStackTrace();
        }
    }
}

}

1 个答案:

答案 0 :(得分:0)

这样做-

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