【问题标题】:com.google.ads.AdView failed to instantiatecom.google.ads.AdView 无法实例化
【发布时间】:2023-03-23 07:35:01
【问题描述】:

我使用了 admob sdk 并添加为外部 jar。在我指定的 xml 中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
<TextView android:id="@+id/headerfield"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">
<ListView android:id="@+id/list"
    android:layout_width="fill_parent"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

<com.google.ads.AdView 
    android:id="@+id/ad"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    ads:adSize="BANNER"    
    ads:adUnitId="Publisher Id"
    ads:loadAdOnCreate="true" />    
</LinearLayout>

清单

<activity android:name="com.google.ads.AdActivity"
    android:configChanges="keyboard|keyboardHidden|orientation"/>

但在图形模式下,它会给我一个错误日志:

com.google.ads.AdView - the following class could not be instantiated. 

在程序内部显示

您必须在 AndroidManifest.xml 中使用 configChanges 声明 AdActivity。

【问题讨论】:

  • &lt;?xml version="1.0" encoding="utf-8"?&gt; &lt;br/&gt; &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads" android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/bgshare"&gt;
  • &lt;ListView android:id="@+id/list" android:layout_width="fill_parent" android:layout_height="wrap_content"&gt; 然后这是问题内指定的广告视图,然后关闭相对布局
  • 看起来很奇怪 - 您的 AdView 在 ListView 中吗?正如 becomputer 建议的那样,在您的问题中发布整个布局文件。
  • 我的布局在我调用 adView 的程序中运行良好,它显示的 AdActivity 应该在 AndroidManifest 中声明 configChanges,即使我已经编写了 configChanges

标签: android xml admob


【解决方案1】:

documentation 开始,您需要在活动中包含额外的 configChanges(假设您使用的是 4.3.1 或更高版本):

<activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>

如果遇到无法识别 screenSize 和 minimumScreenSize 的问题,则必须编译 aganist Android 3.2 或更高版本。查看此migration blog post,了解有关从 4.1.1 升级到更新版本的更多信息(同样适用于升级到 6.0.0)。

【讨论】:

  • 是的,我也试过这个,但整个语句给了我错误,我给了三个参数,前三个 keyboard|keyboardHidden|orientation。我想我会尝试更高的目标版本。
  • 您可能会遇到错误,因为无法识别 screenSize 和 minimumScreenSize 配置更改。它们仅在 API 13 中引入,因此您需要将 project.properties 中的目标设置为 android-13 或更高版本才能针对最近的 SDK 进行编译。 SDK 对 元素没有任何限制,因此您仍然可以将 minSdkVersion 和 targetSdkVersion 设置为您想要的任何内容,并将您的应用程序提供给这些设备。
  • Eric 在这里的回复是正确的。您的问题几乎可以肯定是您没有针对足够高的 API 版本进行编译。
  • 尽管它在文档中说 api 级别 13,但在我更改为 api 级别 15 之前,此错误不会消失。不过没什么大不了的
【解决方案2】:

您是否也导出了 AdMob 库?

进入项目属性 > Java 构建路径 > 排序和导出

检查 admob.jar 旁边的小框是否已选中;如果没有,请这样做。

【讨论】:

  • order和export下勾选复选框
  • 您是否还在 XML 文件的顶部元素中设置了 xmlns:ads?
【解决方案3】:

如果有人仍在查看此线程,请检查您渲染视图的 API 级别是否低于 17。对我来说,它在 17 时工作。您可以使用图形布局上方的小机器人来更改它。

【讨论】:

  • 请解释一下你的答案
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-03-30
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-03
  • 2016-06-09
  • 2020-10-26
相关资源
最近更新 更多