[英]Cannot resolve method findViewById(); while adding admob
I learnt to integrate Admob banner ads, In main activity i wrote this code: 我学会了集成Admob标语广告,在主要活动中,我编写了以下代码:
AdView mAdView = (AdView) findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
And i also wanted the ads in another activity, So i copy pasted it. 而且我还希望在其他活动中投放广告,因此我将其粘贴粘贴。 But i get a error as: 但我得到一个错误:
error: cannot find symbol AdView mAdView = (AdView) findViewById(); 错误:找不到符号AdView mAdView =(AdView)findViewById();
Cannot resolve method 无法解决方法
symbol: method findViewById() 符号:方法findViewById()
Error-Image I copy pasted this code in many other class files but it shows the same error.And it only works in MainActivity.java 我复制的错误图片将此代码粘贴到许多其他类文件中,但它显示了相同的错误,并且仅在MainActivity.java中有效
i want to add ads under the textView. 我想在textView下添加广告。 picture 图片
add this in your CustomPagerAdapter 将此添加到您的CustomPagerAdapter中
@Override
public Object instantiateItem(ViewGroup collection, final int position) {
LayoutInflater inflater = LayoutInflater.from(mContext);
View layout = inflater.inflate(R.layout.chat_layout, collection, false);
RecyclerView recyclerView = (RecyclerView) layout.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(mContext));
AdView mAdView = (AdView) layout.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
mAdView.loadAd(adRequest);
}
//Just copy and paste the additional 3 lines code into your file.
implementation 'com.google.android.gms:play-services-ads:17.2.0'
implementation 'com.android.support:customtabs:28.0.0'
implementation 'com.android.support:support-media-compat:28.0.0'
implementation 'com.android.support:support-v4:28.0.0'
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.