简体   繁体   中英

Can't get Admob to work

 import com.google.android.gms.ads.AdRequest;
 import com.google.android.gms.ads.AdView;

It says cannot resolve symbol AdRequest and cannot resolve Adview. What am I doing wrong? Included the following in my main activity file

    <com.google.android.gms.ads.AdView
        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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView><com.google.android.gms.ads.AdView
        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="@string/banner_ad_unit_id">
    </com.google.android.gms.ads.AdView>

Also, I've added the following to Build.gradle

dependencies {


compile 'com.android.support:support-v4:23.1.1'
compile 'com.google.android.gms:play-services-ads:8.3.0'
}

I want to display Banner Ads on my app and I'm using Admob for the first time. Added above codes from google dev website. In the app's project structure, I've also enabled Admob.

EDIT

I now know what's the problem but cannot fix it. I've enabled Admob from Project Structure and it has added

       compile 'com.google.android.gms:play-services:8.4.0'

in the build.gradle. I've checked a banner example, Example has play-services in external libraries but it doesn't show up in my app.

In my app, play-services only shows up in build.gradle but not in the External Libraries. I only see Android API 23 Platform, < 1.8 >, android-android-23 and support-annotations in my External Libraries column. I tried cleaning the project, rebuilding and also tried Invalidate cache/ Restart. None of them worked. Any suggestions?

I had same issue in my last project and I solved it like this

Goto Build menu

Click on Clean Project

let it finish then Build menu again and hit Rebuild Project

If this didn't solve it.

Try this

you need to edit your manifest file

ad this after application tag

   <meta-data
        android:name="com.google.android.gms.version"
        android:value="@integer/google_play_services_version" />


    <!-- Include the AdActivity configChanges and theme. -->
    <activity
        android:name="com.google.android.gms.ads.AdActivity"
        android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"
        android:theme="@android:style/Theme.Translucent" />

EDIT

Download this example provided by google in their tutorial here and see if it works or not and let us know what was the result

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM