TECHY GLITCHER
TECHY GLITCHER

Reputation: 41

Cannot resolve method findViewById(); while adding admob

I learnt to integrate Admob banner ads, In main activity i wrote this code:

    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();

Cannot resolve method

symbol: method 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

i want to add ads under the textView.picture

Upvotes: 0

Views: 1067

Answers (3)

Md Kamrul Hasan
Md Kamrul Hasan

Reputation: 1

//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'

Upvotes: 0

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

add this in your 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);

}

Upvotes: 1

Saurabh Mistry
Saurabh Mistry

Reputation: 13669

Add Adview to your activity_layout.xml

enter image description here

Add this Code in your Activity onCreate method.

enter image description here

Upvotes: 1

Related Questions