Admob 实施错误 [英] Admob implementation Error

查看:19
本文介绍了Admob 实施错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在将 Admob 应用到应用中时遇到了问题.

这是我的 main.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"机器人:方向=垂直"><文本视图android:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="大文本"android:textAppearance="?android:attr/textAppearanceLarge"/><线性布局android:id="@+id/linearLayout1"android:layout_width="fill_parent"android:layout_height="wrap_content"机器人:方向=垂直"></LinearLayout></LinearLayout>

这是我的清单:

<块引用>

<manifest xmlns:android="http://schemas.android.com/apk/res/android"包=de.ollidiemaus.testmob"机器人:版本代码=1"android:versionName="1.0" ><使用-sdk android:minSdkVersion="7"/><申请android:icon="@drawable/ic_launcher"android:label="@string/app_name" ><活动android:label="@string/app_name"android:name=".TestAdmobActivity" ><意图过滤器><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></意图过滤器></活动><activity android:name="com.google.ads.AdActivity"android:configChanges="keyboard|keyboardHidden|方向"/></应用程序><uses-permission android:name="android.permission.INTERNET"/><uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/></清单>

最后这是我的活动:

<块引用>

公共类 TestAdmobActivity 扩展 Activity {私人 AdView adView;/** 在第一次创建活动时调用.*/@覆盖public void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);//创建广告视图adView = new AdView(this, AdSize.BANNER, "a14ead58dc2a456");//查找你的 LinearLayout 假设它已经给出//属性 android:id="@+id/mainLayout"LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);//向其中添加 adViewlayout.addView(adView);//发起一个通用请求来加载一个广告adView.loadAd(new AdRequest());}@覆盖公共无效 onDestroy() {adView.destroy();super.onDestroy();}}

现在,当我在 AdView 中启动应用程序时,会出现一个错误,称为:您必须在 AndroidManifest.xml 中使用 configChanges 声明 AdActivity."

我使用 Android 4.0,所以高于 3.2 但它不起作用.我希望有人能帮助我.

解决方案

我遇到了完全相同的问题,即您必须使用 configChanges 在 AndroidManifest.xml 中声明 AdActivity".集成最新的 AdMob SDK 后出现错误消息.尽管我在 StackOverflow 上找到了这个和其他两个相关的讨论(请参阅本文底部的链接),但它们并没有帮助我解决这个问题,因为 - 对我来说 - 他们没有足够清楚地区分清单和构建目标中的 targetSdkVersion 属性.这个答案描述了是什么为我解决了问题以及是什么导致了问题.

解决方案

最简单的部分:您在 AndroidManifest.xmlAdActivity 定义的 configChanges 属性中缺少一些标志>.如AdMob SDK 文档 定义需要如下所示:

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

第二个——也是更复杂的部分——与 SDK 目标相关:唯一似乎真正有效的解决方案是使用 SDK 管理器安装至少适用于 Android 3.2(API 级别 13)的 SDK.安装此 SDK 版本后,您需要配置 IDE 以使用此 SDK 构建项目.确切的设置取决于您使用的 IDE.就我而言,它是 IntelliJ IDEA,您会在 Project 页面的项目设置中的标题 Project SDK 下方找到该选项.

您还应该调整项目project.properties 中的target 属性.如果您使用 ANT 构建您的版本,这至少很重要.该行应如下所示:

target=android-13

仅上述配置就可以解决问题.AndroidManifest 中的 元素不需要更改.阅读下面的陷阱,了解这可能会导致问题的原因.

说明

构建目标和 元素具有完全不同的范围.

构建目标在构建时由您的构建工具评估,以确定应该使用您系统上的哪个版本的 SDK 工具来构建您的应用.SDK 越新,它知道的 API 功能就越多.出于某种原因,Google 强制我们指定一些在 API 级别 13 之前不可用的 configChanges,因此我们需要至少使用 SDK 工具 13 构建我们的应用程序,因为以前版本的 SDK 工具不知道这些新的configChanges会报错.在运行时,构建目标没有任何意义,Android 将忽略它不知道的所有元素(例如在 configChanges 中).

相比之下,android 清单中 元素上指定的 targetSdkVersion 元素在运行时评估 -- 不是在编译时.事实上,您可以在此处指定您想要的任何值,而无需编译器更改任何内容或显示错误消息.这就是为什么更改此属性并不能帮助我们解决 AdMob 问题的原因.另一方面,在运行时,android 可能会评估该属性,以支持为旧 android 版本构建的应用程序的某些兼容性功能.请参阅下面的陷阱部分,了解给我带来麻烦的主题.

陷阱

  • 如果您无法在此 android 版本上测试您的应用程序,请不要不要targetSdkVersion 设置更改为更高版本:因为我有误解了关于这个 admob 主题的现有答案我还在我的应用程序中将 <uses-sdk> 元素的 android:targetSdkVersion 属性设置为 API 级别 13,这导致了致命的副作用:由于 Android 3 认为我的应用程序本机支持蜂窝,它不再在底部边框的软按钮栏中显示菜单按钮,并且由于我的应用程序隐藏了本机标题栏以显示它自己的实现,用户没有有任何可能在蜂窝上访问菜单.因此,对我来说,将 targetSdkVersion 留在 10 级别有助于恢复菜单按钮并正常工作.
  • 处理可能在 SDK 工具 13 中丢失的旧 API 的向后兼容性:好的,所以我已将构建过程设置为使用 SDK 工具 13 和我的 targetSdkVersion到 10,一切都应该没问题,对吧?不幸的是没有!原因是,我的应用向下兼容 Android 1.5(API 级别 3),因为这仍然占我用户的 5% 左右.不幸的是,在将构建目标设置为 13 后,我的代码的某些部分不再编译,因为它们引用了不推荐使用的方法,这些方法在 SDK 工具 10 之前支持但不再从 SDK 工具 11 开始(例如 Service.setForeground).

描述如何处理向后兼容性的基础知识here -- 但本文没有描述如何调用不再可用的弃用方法,因为它们会导致编译器错误.我通过创建一个由我的应用程序使用的新库项目解决了这个问题.对于这个库项目,我已将构建目标设置回 10,这将导致它使用 SDK 工具 10 进行编译,该工具仍然了解我正在使用的 android 弃用 API.然后我的应用会从这个兼容性库调用辅助方法,这样我就可以使用更新的目标 SDK 编译我的应用.

<小时>

相关主题

这是我发现的其他相关讨论的列表:

I got a Problem with implementing Admob into an App.

This is my main.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"
android:orientation="vertical" >
<TextView
    android:id="@+id/textView1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Large Text"
    android:textAppearance="?android:attr/textAppearanceLarge" />
<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
</LinearLayout>
</LinearLayout>

and this is my Manifest:

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="de.ollidiemaus.testmob"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk android:minSdkVersion="7" />
<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" >
    <activity
        android:label="@string/app_name"
        android:name=".TestAdmobActivity" >
        <intent-filter >
            <action android:name="android.intent.action.MAIN" />
            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
        <activity android:name="com.google.ads.AdActivity"
          android:configChanges="keyboard|keyboardHidden|orientation"/>
</application>
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
</manifest>

And finaly this is my Activity:

public class TestAdmobActivity extends Activity {
private AdView adView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    // Create the adView
adView = new AdView(this, AdSize.BANNER, "a14ead58dc2a456");
// Lookup your LinearLayout assuming it’s been given
// the attribute android:id="@+id/mainLayout"
LinearLayout layout = (LinearLayout)findViewById(R.id.linearLayout1);
// Add the adView to it
layout.addView(adView);
// Initiate a generic request to load it with an ad
adView.loadAd(new AdRequest());}    
@Override
public void onDestroy() {
  adView.destroy();
  super.onDestroy();
}}

Now when i'll start the app in the AdView is an error called: "you must have AdActivity declared in AndroidManifest.xml with configChanges."

I used Android 4.0 so above 3.2 but it is not working. I hope anyone could help me.

解决方案

I had exactly the same problem with getting the "you must have AdActivity declared in AndroidManifest.xml with configChanges." error message after integrating the latest AdMob SDK. Even though I have found this and the other two related discussions (see links at the bottom of this article) at StackOverflow, they didn't help me to solve the issue, as -- for me -- they did not differentiate clear enough between the targetSdkVersion attribute in the manifest and the build target. This answer describes what solved the issue for me and what caused trouble.

Solution

The easiest part at first: you are missing a few flags in the configChanges attribute of the definition of the AdActivity in your AndroidManifest.xml. As shown in the AdMob SDK Docs the definition needs to look like this:

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

The second -- and more complicated part -- is related to the SDK target: The only solution that really seems to work is to use the SDK manager to install the SDK for at least Android 3.2 (API level 13). After you have installed this SDK version you need to configure your IDE to build your project using this SDK. The exact setting depends on the IDE you are using. In my case it is IntelliJ IDEA and there you will find the option in the project settings on the Project page below the headline Project SDK.

You should also adjust the target property in your project's project.properties. This is at least important if you are building your release using ANT. The line should look like this:

target=android-13

The configuration above alone does the trick. There are no changes required to the <uses-sdk> element in the AndroidManifest. Read the pitfalls below to learn why this may cause trouble.

Explanation

The build target and the <uses-sdk> elements have completely different scopes.

The build target is evaluated only at build time by your build tools to determine which version of the SDK tools on your system should be used to build your app. The newer the SDK the more API features does it know. Out of whatever reason Google forces us to specify some configChanges which aren't available before API level 13 and so we need to build our app using at least the SDK tools 13 because a previous version of the SDK tools does not know these new configChanges and will report an error. At runtime the build target does not have any meaning and Android will ignore all elements (e.g. in configChanges) which it does not know.

The targetSdkVersion element specified on the <uses-sdk> element in the android manifest in contrast is only evaluated at run time -- not at compile time. In fact you can specify any value you want here without the compiler changing anything or bringing up an error message. This is why changing this attribute does not help us to solve our AdMob problem. On the other hand at runtime the attribute may be evaluated by android to support some compatibility features for apps which have been build for older android versions. See the pitfalls section below to see a topic which caused trouble for me.

Pitfalls

  • Do not change set targetSdkVersion to a higher version if you are not able to test your app on this android version: Because I have misunderstood the existing answers regarding this admob topic I have also set the android:targetSdkVersion attribute of the <uses-sdk> element to API level 13 in my app which caused a fatal side effect: As Android 3 thought my app would natively support honeycomb, it did no longer show the menu button in the soft button bar at the bottom border and as my app hides the native title bar to show it's own implementation the user did not have any possibility to access the menu any more on honeycomb. So for me leaving the targetSdkVersion at level 10 helped to bring back the menu button and worked fine.
  • Handling backward compatibility to older APIs which may be lost in SDK tools 13: OK, so I have set my build process to use the SDK tools 13 and my targetSdkVersion to 10 and everything should be fine, right? Unfortunately not! The reason is, that my app is compatible downwards to Android 1.5 (API level 3) as this still makes up about 5% of my users. Unfortunately after setting the build target to 13 some parts of my code did not compile any more as they referenced deprecated methods which were supported until SDK tools 10 but no longer starting with SDK tools 11 (e.g. Service.setForeground).

The basics how to handle backwards compatibility are described here -- but this article does not describe how to call deprecated methods which are no longer available as they will cause a compiler error. I solved this by creating a new library project used by my app. For this library project I have set the build target back to 10 which will cause it to be compiled using the SDK tools 10 which still know about the android deprecated API I am using. My app then calls helper methods from this compatibility library and so I can compile my app with a newer target SDK.


Related Topics

Here the list of the other related discussions I have found:

这篇关于Admob 实施错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆