[英]How to add admob ads in fragments in android studio?
I would love to add admob ads to fragments in my app.我很想在我的应用程序的片段中添加 admob 广告。 I have already added the ads in the XML files.
我已经在 XML 文件中添加了广告。 I am having trouble in the java part, however.
但是,我在 java 部件中遇到了麻烦。 Could you please share the java code with rootview?
您能否与 rootview 分享 java 代码? I have checked other articles but I am confused.
我检查了其他文章,但我很困惑。
HomeFragment.java首页Fragment.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将此添加到 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>
However, it return an error with the findViewById part.但是,它会返回 findViewById 部分的错误。 And I would also like to know what should be added to the Manifest file.
而且我还想知道应该在清单文件中添加什么。
You have to run this on your Activity
:你必须在你的
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
:这在你的
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);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.