简体   繁体   中英

AdView Admob Error

I have seen many similar topics at this forum, but none of them solved my problem. In my code I have something like this:

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

...

AdView adView = (AdView)this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder()
.addTestDevice("TEST_DEVICE_ID")
.build();
adView.loadAd(adRequest);

And it shows me 2 errors(I marked the red marks as bold text):

-In first line: "AdView adView = (AdView)this.findViewById(R.id. adView );" - adView cannot be resolved or is not a field

-And in last line: " adView.loadAd(adRequest) ;" - The type android.view.ViewGroup cannot be resolved. It is indirectly referenced from required .class files

I have no idea what causes it. I also had a problem with lack of "layout" folder before, but I generated it with eclipse new>other>android XML layout file. Should I also somehow link it using manifest?

PS It's libGDX project

EDIT: Here is my layout/activity_main.xml file:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

<com.google.android.gms.ads.AdView
    android:id="@+id/adView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id">

</com.google.android.gms.ads.AdView>

your problem is with this line

import com.google.android.gms.R;

remove this and import your package .R file

import <package name>.R

I have same issue in cocos2dx, I don't have layout file and I want to dislplay admob banner ads, please try this solution without creating layout xml file:

inside onCreate function :

        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId("YOUR ID");

        AdRequest adRequest = new AdRequest.Builder().build();

        adView.loadAd(adRequest);

        adView.setBackgroundColor(Color.BLACK);
        adView.setBackgroundColor(0);
        getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

        int width = getDisplaySize(getWindowManager().getDefaultDisplay()).x;

        LinearLayout.LayoutParams adParams = new LinearLayout.LayoutParams(
                width, LinearLayout.LayoutParams.WRAP_CONTENT);
        addContentView(adView, adParams);

and getDisplaySize method:

// Helper get display screen to avoid deprecated function use
        private Point getDisplaySize(Display d)
            {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB)
                {
                    return getDisplaySizeGE11(d);
                }
                return getDisplaySizeLT11(d);
            }

            @TargetApi(Build.VERSION_CODES.HONEYCOMB_MR2)
            private Point getDisplaySizeGE11(Display d)
            {
                Point p = new Point(0, 0);
                d.getSize(p);
                return p;
            }
            private Point getDisplaySizeLT11(Display d)
            {
                try
                {
                    Method getWidth = Display.class.getMethod("getWidth", new Class[] {});
                    Method getHeight = Display.class.getMethod("getHeight", new Class[] {});
                    return new Point(((Integer) getWidth.invoke(d, (Object[]) null)).intValue(), ((Integer) getHeight.invoke(d, (Object[]) null)).intValue());
                }
                catch (NoSuchMethodException e2) // None of these exceptions should ever occur.
                {
                    return new Point(-1, -1);
                }
                catch (IllegalArgumentException e2)
                {
                    return new Point(-2, -2);
                }
                catch (IllegalAccessException e2)
                {
                    return new Point(-3, -3);
                }
                catch (InvocationTargetException e2)
                {
                    return new Point(-4, -4);
                }
            }

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