Admob的执行错误 [英] Admob implementation Error

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

问题描述

我有一个问题与实现Admob的到的应用程序。

这是我的main.xml:

 < LinearLayout中的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
的xmlns:广告=htt​​p://schemas.android.com/apk/lib/com.google.ads
机器人:layout_width =FILL_PARENT
机器人:layout_height =FILL_PARENT
机器人:方向=垂直>
<的TextView
    机器人:ID =@ + ID / textView1
    机器人:layout_width =WRAP_CONTENT
    机器人:layout_height =WRAP_CONTENT
    机器人:文本=大文本
    机器人:textAppearance =:/>中的Andr​​oid ATTR / textAppearanceLarge?
<的LinearLayout
    机器人:ID =@ + ID / linearLayout1
    机器人:layout_width =FILL_PARENT
    机器人:layout_height =WRAP_CONTENT
    机器人:方向=垂直>
< / LinearLayout中>
< / LinearLayout中>
 

这是我的清单:

 <舱单的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
包=de.ollidiemaus.testmob
安卓版code =1
机器人:VERSIONNAME =1.0>
<使用-SDK安卓的minSdkVersion =7/>
<应用
    机器人:图标=@可绘制/ ic_launcher
    机器人:标签=@字符串/ APP_NAME>
    <活动
        机器人:标签=@字符串/ APP_NAME
        机器人:名称=TestAdmobActivity。>
        <意向滤光器>
            <作用机器人:名称=android.intent.action.MAIN/>
            <类机器人:名称=android.intent.category.LAUNCHER/>
        &所述; /意图滤光器>
    < /活性GT;
        <活动机器人:名称=com.google.ads.AdActivity
          机器人:configChanges =键盘| keyboardHidden |定位/>
< /用途>
<使用-权限的Andr​​oid:名称=android.permission.INTERNET对/>
<使用-权限的Andr​​oid:名称=android.permission.ACCESS_NETWORK_STATE/>
< /舱单>
 

和finaly这是我的活动:

 公共类TestAdmobActivity延伸活动{
私人AD浏览报AD浏览报;
/ **第一次创建活动时调用。 * /
@覆盖
公共无效的onCreate(包savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.main);
    //创建AD浏览报
AD浏览报=新的AD浏览报(这一点,AdSize.BANNER,a14ead58dc2a456);
//查找您的LinearLayout假设它是被赋予了
//属性机器人:ID =@ + ID / mainLayout
的LinearLayout布局=(的LinearLayout)findViewById(R.id.linearLayout1);
// AD浏览报给它添加
layout.addView(AD浏览报);
//初始化一个通用的请求与广告加载
adView.loadAd(新AdRequest());}
@覆盖
公共无效的onDestroy(){
  adView.destroy();
  super.onDestroy();
}}
 

现在,当我将开始在AD浏览报的应用程序被称为一个错误:你必须有AdActivity在AndroidManifest.xml中宣布与configChanges

我用的Andr​​oid 4.0这样在3.2以上,但它不能正常工作。 我希望有人可以帮助我。

解决方案

我有完全一样的问题,让你必须有AdActivity在AndroidManifest.xml中声明与configChanges。集成了最新的AdMob的SDK后,错误信息。尽管我发现这和其他两个相关讨论(见本文底部链接)的计算器,他们没有帮我解决了问题,因为 - 我 - 他们没有区分不够清楚在 targetSdkVersion 属性在清单和构建目标。这个答案描述了解决这个问题,我和什么原因造成的麻烦。

解决方案

首先最容易的部分:你缺少几个标志在 AdActivity configChanges 属性>在的Andr​​oidManifest.xml 。如图中 AdMob的SDK文档的定义必须是这样的:

 <活动机器人:名称=com.google.ads.AdActivity
android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
 

第二个 - 更复杂的部分 - 是相关的SDK目标:真正似乎工作,唯一的解决办法是使用SDK管理器来安装SDK最少的Andr​​oid 3.2(API级别13)。当你安装这个SDK版本,你需要配置你的IDE使用该SDK构建项目。确切的设置取决于您使用的IDE。在我的情况下,它是IntelliJ IDEA的有,你会发现在项目设置的选项中的项目的标题页面下方的项目SDK 的。

您也应该调整目标属性在项目的 project.properties 。这至少是重要的,如果您正在构建您的版本中使用ANT。该行应该是这样的:

 目标=机器人-13
 

以上单独配置的伎俩。还有的需要的&LT没有的变化;采用-SDK> 在AndroidManifest元素。阅读下面的陷阱,了解为什么这可能会引起麻烦。

说明

构建目标和<使用-SDK> 元素具有完全不同的范围。

构建目标被评估的只有的在制作的时候你生成工具,以确定哪个版本的系统上的SDK工具应该用于构建应用程序。该SDK的更新更多的API功能,它知道。出是什么原因谷歌迫使我们来指定一些 configChanges 这是不可用的API级别13之前,因此我们需要至少使用SDK工具13,因为建立我们的应用程序的SDK工具previous版本不知道这些新的 configChanges 键,将报告错误。在运行时创建目标没有任何意义和Android会忽略所有的元素(如 configChanges ),它不知道。

targetSdkVersion 在指定元素<使用-SDK> 在对比Android清单元素的只有评估在运行时 - 的没有的在编译时。事实上,你可以指定你想在这里的任何值,而不需要编译器改变任何东西,或把一则错误信息。这就是为什么改变这个属性并不能帮助我们解决我们的AdMob的问题。另外在运行时的属性可通过机器人进行评估,以支持已建立为老年人的Andr​​oid版本的应用程序兼容性的一些功能。请参阅下面的陷阱一节,看看这引起麻烦,我的一个话题。

陷阱

  • 你的没有的变更集 targetSdkVersion 到更高的版本,如果你不能够测试这个安卓版本的应用程序:因为我误解了关于这个AdMob的话题我也设置了的Andr​​oid现有的答案:targetSdkVersion 的属性<使用-SDK> 元素API级别13在我的应用程序,它造成了致命的副作用:由于Android的3以为我的应用程序将本地支持蜂窝,它也不再是在底边框显示的软按钮栏菜单按钮并作为我的应用程序隐藏本机的标题栏显示它自己的实现用户没有任何可能进入菜单更多的蜂窝。所以对我来说离开 targetSdkVersion 在级别 10 帮助带回菜单按钮,正常工作。
  • 处理向后兼容旧的API可能会丢失SDK工具13:确定,所以我把我的构建过程中使用SDK工具13,我的 targetSdkVersion 10,一切都应该罚款,对不对?不幸的是没有!其原因是,我的应用程序是兼容下调至了Android 1.5(API等级3),因为这仍使约5%,我的用户。不幸的是,构建目标设定为13后,我的code一些地方没有编译任何更多,因为他们引用这得到了支持,直到SDK工具10,但不再开始SDK工具11(例如<$ C德precated方法$ C> Service.setForeground )。

基础知识如何处理向后兼容性描述这里 - 但本文不介绍如何调用去precated方法,它们不再有效,因为它们会导致编译器错误。我通过创建使用我的应用程序一个新的库项目解决了这一点。对于这个库项目,我已经设置了构建目标回10,这将导致它使用SDK工具10仍然知道我使用了android德precated 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天全站免登陆