我尝试将 DFP 横幅广告添加到我的应用程序中,所有步骤都已在https://developers.google.com/ad-manager/mobile-ads-sdk/android/banner中应用。
但是如果我打开我的应用程序它会崩溃
碰撞:
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.google.android.gms.ads.doubleclick.PublisherAdView.loadAd(com.google.android.gms.ads.doubleclick.PublisherAdRequest)' on a null object reference
at com.englishprofor.all.fragment.FragmentCategory.onCreateView(FragmentCategory.java:61)
FragmentCategory.class
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import com.englishprofor.all.AppController;
import com.englishprofor.all.Constant;
import com.englishprofor.all.R;
import com.englishprofor.all.activity.MainActivity;
import com.englishprofor.all.activity.SettingActivity;
import com.englishprofor.all.helper.CircleImageView;
import com.englishprofor.all.helper.SettingsPreferences;
import com.englishprofor.all.model.Category;
import com.englishprofor.all.model.SubCategory;
import com.google.android.gms.ads.doubleclick.PublisherAdRequest;
import com.google.android.gms.ads.doubleclick.PublisherAdView;
import com.google.android.gms.ads.doubleclick.*;
import java.util.ArrayList;
public class FragmentCategory extends Fragment {
private static final String TAG = "FragmentCategory";
Context con ;
private RecyclerView recyclerView;
private View view;
ArrayList<Category> mListItem;
//AdView mAdView;
private PublisherAdView mPublisherAdView;
TextView empty_msg, tvTitle;
CoordinatorLayout layout;
ImageView back, setting;
public static ArrayList<Category> categoryList;
public static ArrayList<SubCategory> subCatList;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_category, container, false);
mPublisherAdView = view.findViewById(R.id.publisherAdView);
PublisherAdRequest adRequest = new PublisherAdRequest.Builder().build();
mPublisherAdView.loadAd(adRequest);
片段类别.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:ads="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/background">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include layout="@layout/actionbar_layout"/>
<android.support.v7.widget.RecyclerView
android:id="@+id/category_recycleview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/relativeLayout"
android:layout_above="@+id/banner_AdView"/>
<TextView
android:id="@+id/txtblanklist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:textColor="@color/black"
android:textSize="15dp"
android:textStyle="bold"
android:visibility="gone" />
<com.google.android.gms.ads.doubleclick.PublisherAdView
android:id="@+id/banner_AdView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
ads:adSize="BANNER"
ads:adUnitId="/6499/example/banner">
</com.google.android.gms.ads.doubleclick.PublisherAdView>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
请帮帮我,我想在我的应用中启用 DFP
谢谢大家