Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在调用loadAd之前,必须设置Ad大小和Ad单元id_Java_Android_Admob - Fatal编程技术网

Java 在调用loadAd之前,必须设置Ad大小和Ad单元id

Java 在调用loadAd之前,必须设置Ad大小和Ad单元id,java,android,admob,Java,Android,Admob,我的主xml文件中有以下内容: <com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:layout_width="wrap_content" android:layout_height="wrap_content" ads:adUnitId="******************"

我的主xml文件中有以下内容:

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:adUnitId="******************"
        ads:adSize="BANNER"
        android:id="@+id/adView"/>

它抛出了标题中的异常。

这将是模糊的,因为您没有说明XML文件的视图类型(相对视图、线性视图等)

在具有可滚动相对布局的应用程序中,我包括:

<RelativeLayout
    android:id="@+id/test"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_below="@+id/TextView07"
    android:paddingTop="20dp" >
</RelativeLayout>
private AdView adView;

/* Your ad unit id. Replace with your actual ad unit id. */
private static final String AD_UNIT_ID = "ca-app-pub-xxxxxx/yyyyyy";

/**
 * Simply loads the xml about page layout to display the text.
 */

public void onCreate(Bundle start) {
    super.onCreate(start);
    setContentView(R.layout.about);

    final TelephonyManager tm = (TelephonyManager) getBaseContext()
            .getSystemService(Context.TELEPHONY_SERVICE);

    //String deviceid = tm.getDeviceId();

    adView = new AdView(this);
    adView.setAdSize(AdSize.SMART_BANNER);
    adView.setAdUnitId(AD_UNIT_ID);

    // Add the AdView to the view hierarchy. The view will have no size
    // until the ad is loaded.
    RelativeLayout layout = (RelativeLayout) findViewById(R.id.test);
    layout.addView(adView);

    // Create an ad request. Check logcat output for the hashed device ID to
    // get test ads on a physical device.
    AdRequest adRequest = new AdRequest.Builder()
    .build();
            //.addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            //.addTestDevice(deviceid).build();

    // Start loading the ad in the background.
    adView.loadAd(adRequest);
}
现在,理想情况下,您希望使用智能横幅优化所有设备的布局,正如我上面所述,这是现在的标准


如果没有帮助,请转到我在github示例中找到的解决方案,即:

而不是

xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
删除xmlns:ads***标记并添加

xmlns:ads="http://schemas.android.com/apk/res-auto"
标记到线性布局标记,如下所示:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          xmlns:ads="http://schemas.android.com/apk/res-auto"
          android:orientation="vertical"
          android:layout_width="match_parent"
          android:layout_height="match_parent">
<TextView android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:text="@string/hello"/>
<com.google.android.gms.ads.AdView android:id="@+id/adView"
                       android:layout_width="match_parent"
                       android:layout_height="wrap_content"
                       ads:adSize="BANNER"
                       ads:adUnitId="INSERT_YOUR_AD_UNIT_ID_HERE"/>
</LinearLayout>

就这样:)


许多将现有应用程序迁移到google play services方法的人错误地添加了错误的信息

我想指出的是,许多有相同问题的人都将此作为模板

<com.google.android.gms.ads.AdView
    xmlns:ads="http://schemas.android.com/apk/lib-auto"
    android:id="@+id/adView"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    ads:adSize="BANNER"
    ads:adUnitId="ca-app-pub-XXXXXXXXXXXXXXXX/XXXXXXXXXX"
    ads:loadAdOnCreate="true" >
</com.google.android.gms.ads.AdView>

我试图更改名称空间声明,但它就是不起作用。我通过为adView指定一个高度值而不是
wrap\u content
修复了这个问题。它对我有用

<com.google.android.gms.ads.AdView
    android:id="@+id/adView2"
    android:layout_width="match_parent"
    android:layout_height="50dp"
    android:layout_alignParentBottom="true"
    android:layout_centerHorizontal="true"
    android:layout_gravity="center|bottom"
    ads:adSize="BANNER"
    ads:adUnitId="@string/banner_ad_unit_id" />

警告:确保以相同的方式设置广告大小和广告单元ID(即以XML或编程方式设置两者)


删除
xmlns:ads=”http://schemas.android.com/tools“
从根目录视图查看

<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        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="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>
然后添加AdView

<com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        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="ca-app-pub-3940256099942544/6300978111">
    </com.google.android.gms.ads.AdView>

请记住以下步骤:

  • 您可以定义
    xmlns:ads=”http://schemas.android.com/apk/res-auto"
    
    在根目录上查看它自己

  • 如果尚未定义
    xmlns:ads=”http://schemas.android.com/apk/res-auto“
    在rootView内部,则必须添加
    xmlns:ads=”http://schemas.android.com/apk/res-auto“
    insideAdView.

  • 如果在
    AdView
    xml文件中定义了adSize,则不要动态设置,例如:
    AdView.setAdSize(adSize.BANNER)
    或者它将通过异常
    java.lang.IllegalStateException:广告大小只能在AdView上设置一次。

  • xmlns:ads=”http://schemas.android.com/apk/res-auto“
    xmlns:app=”http://schemas.android.com/apk/res-auto“
    看起来是一样的,但有一点不同,那就是xmlns:adsxmlns:app,所以不要混淆


  • 我想这会解决你的问题。。。很乐意帮助:)

    我也遇到了同样的问题,因为我没有在
    xmlns:ads=”上添加这一行http://schemas.android.com/apk/res-auto“
    。然后像这样更改xml代码

        <com.google.android.gms.ads.AdView
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        ads:layout_constraintLeft_toLeftOf="parent"
        ads:layout_constraintRight_toRightOf="parent"
        ads:layout_constraintTop_toTopOf="parent"
        ads:layout_constraintBottom_toTopOf="@+id/headerText"
        android:layout_marginTop="@dimen/_2sdp"
        ads:adSize="BANNER"
        ads:adUnitId="ca-app-pub-3940256099942544/6300978111"
        android:visibility="visible">
    </com.google.android.gms.ads.AdView>
    

    对于任何感兴趣的人,这里有一个Kotlin版本

    <RelativeLayout
        android:id="@+id/adContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">
    </RelativeLayout>
    

    你能发布你的日志吗?你使用的是谷歌Play服务的AdMob吗?这有帮助吗?我以前也做过同样的事情。我想我不会再重做了。谢谢:)谢谢你的救命恩典你节省了很多时间,我从来没有想过这个解决方案。如何通过编程或不使用xml添加AdUnit?你真的让我开心了。更改名称空间是关键!这就是我的问题所在。我从代码中设置了ID,从XML中设置了大小。
    AdView mAdView = (AdView)findViewById(R.id.adView);
        MobileAds.initialize(this, "ca-app-pub-3940256099942544~3347511713");
        AdRequest adRequest = new AdRequest.Builder().build();
        mAdView.loadAd(adRequest);
    
    <RelativeLayout
        android:id="@+id/adContainer"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_alignParentBottom="true">
    </RelativeLayout>
    
    //create a new AdView    
    val adView = AdView(this)
    adView.adSize = AdSize.SMART_BANNER
    adView.adUnitId = "your adUnitID goes here or you can use a function"
    adView.visibility = View.GONE
    
    //add it to the layout
    val layout = adContainer
    layout.addView(adView)
    
    //profit
    MobileAds.initialize(this) {}
    val adRequest = AdRequest.Builder().build()
    adView.loadAd(adRequest)
    adView.adListener = object : AdListener(){
        override fun onAdLoaded() {
            adView.visibility = View.VISIBLE
            super.onAdLoaded()
        }
    }