AdView Admob 错误

AdView Admob Error

我在这个论坛上看到过很多类似的话题,但是 none 解决了我的问题。 在我的代码中,我有这样的东西:

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

它显示了 2 个错误(我将红色标记标记为粗体文本):

-第一行:"AdView adView = (AdView)this.findViewById(R.id.adView);" - adView 无法解析或不是字段

-最后一行:“adView.loadAd(adRequest);” - 无法解析类型 android.view.ViewGroup。它是从所需的 .class 个文件

中间接引用的

我不知道是什么原因造成的。我之前也有缺少 "layout" 文件夹的问题,但是我用 eclipse new>other>android XML 布局文件生成了它。我是否也应该以某种方式 link 使用清单?

P.S。这是 libGDX 项目

编辑: 这是我的 layout/activity_main.xml 文件:

<?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>

我在 cocos2dx 中遇到同样的问题,我没有布局文件,我想显示 admob 横幅广告,请尝试此解决方案而不创建布局 xml 文件:

onCreate 函数内:

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

getDisplaySize方法:

// 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);
                }
            }

你的问题出在这一行

import com.google.android.gms.R;

删除它并导入您的包 .R 文件

import <package name>.R