如何在Android Studio的片段中添加AdMob广告?

huangapple go评论81阅读模式
英文:

How to add admob ads in fragments in android studio?

问题

HomeFragment.java

private AdView mAdView;

@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, rootview);
    mFragmentPositionMap = CommonCodeUtils.getInstance().fillNavigationItemsMap(true);

    mAdView = rootview.findViewById(R.id.adView);  // Changed findViewById to rootview.findViewById
    AdRequest adRequest = new AdRequest.Builder().build();
    mAdView.loadAd(adRequest);

    // Your existing click listeners setup

    mAdapter = new RecentListAdapter(this);
    recentList.setAdapter(mAdapter);
    return rootview;
}

fragment_home.xml

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_alignParentBottom="true"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-3940256099942544/6300978111">
</com.google.android.gms.ads.AdView>

In your Manifest file, make sure to add the following permissions and the Google Play services metadata:

AndroidManifest.xml

<uses-permission android:name="android.permission.INTERNET" />

<!-- Add this within the <application> section -->
<meta-data
    android:name="com.google.android.gms.ads.APPLICATION_ID"
    android:value="YOUR_ADMOB_APP_ID" />

Replace YOUR_ADMOB_APP_ID with your actual AdMob App ID.

Please note that you should replace "ca-app-pub-3940256099942544/6300978111" with your actual Ad Unit ID in the XML layout. Also, ensure that you have the necessary dependencies and imports for AdMob set up correctly in your app.

英文:

I would love to add admob ads to fragments in my app. I have already added the ads in the XML files. I am having trouble in the java part, however. Could you please share the java code with rootview? I have checked other articles but I am confused.

HomeFragment.java

private AdView mAdView;
@Override
public View onCreateView(@NonNull LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    View rootview = inflater.inflate(R.layout.fragment_home, container, false);
    ButterKnife.bind(this, rootview);
    mFragmentPositionMap = CommonCodeUtils.getInstance().fillNavigationItemsMap(true);

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

    imagesToPdf.setOnClickListener(this);
    qrbarcodeToPdf.setOnClickListener(this);
    textToPdf.setOnClickListener(this);
    viewFiles.setOnClickListener(this);
    viewHistory.setOnClickListener(this);
    splitPdf.setOnClickListener(this);
    mergePdf.setOnClickListener(this);
    compressPdf.setOnClickListener(this);
    removePages.setOnClickListener(this);
    rearrangePages.setOnClickListener(this);
    extractImages.setOnClickListener(this);
    mPdfToImages.setOnClickListener(this);
    addPassword.setOnClickListener(this);
    removePassword.setOnClickListener(this);
    rotatePdf.setOnClickListener(this);
    addWatermark.setOnClickListener(this);
    addImages.setOnClickListener(this);
    removeDuplicatePages.setOnClickListener(this);
    invertPdf.setOnClickListener(this);
    zipToPdf.setOnClickListener(this);
    excelToPdf.setOnClickListener(this);
    extractText.setOnClickListener(this);
    addText.setOnClickListener(this);

    mAdapter =  new RecentListAdapter(this);
    recentList.setAdapter(mAdapter);
    return rootview;
    }

Added This To fragment_home.xml

&lt;com.google.android.gms.ads.AdView
    xmlns:ads=&quot;http://schemas.android.com/apk/res-auto&quot;
    android:id=&quot;@+id/adView&quot;
    android:layout_width=&quot;wrap_content&quot;
    android:layout_height=&quot;wrap_content&quot;
    android:layout_centerHorizontal=&quot;true&quot;
    android:layout_alignParentBottom=&quot;true&quot;
    ads:adSize=&quot;BANNER&quot;
    ads:adUnitId=&quot;ca-app-pub-3940256099942544/6300978111&quot;&gt;
&lt;/com.google.android.gms.ads.AdView&gt;

However, it return an error with the findViewById part. And I would also like to know what should be added to the Manifest file.

答案1

得分: 0

在你的 Activity 中,你需要运行以下代码:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });
}

在你的 Fragment 中,需要添加以下代码:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    AdView adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}
英文:

You have to run this on your Activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    MobileAds.initialize(this, new OnInitializationCompleteListener() {
        @Override
        public void onInitializationComplete(InitializationStatus initializationStatus) {}
    });
}

and this on your Fragment:

@Override
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
    super.onViewCreated(view, savedInstanceState);
    AdView adView = findViewById(R.id.adView);
    AdRequest adRequest = new AdRequest.Builder().build();
    adView.loadAd(adRequest);
}

huangapple
  • 本文由 发表于 2020年8月23日 00:51:30
  • 转载请务必保留本文链接:https://go.coder-hub.com/63538742.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定