Admob执行错误 [英] Admob implementation Error

查看:145
本文介绍了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:orientation = 垂直>
< 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>


这是我的清单:


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


而且这是我的活动:


  public class TestAdmobActivity extends Activity {
private AdView adView;
/ **首次创建活动时调用。 * /
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//创建adView
adView = new AdView(this,AdSize.BANNER,a14ead58dc2a456);
//查找您的LinearLayout,假设它已被赋予
//属性android:id =@ + id / mainLayout
LinearLayout layout =(LinearLayout)findViewById(R.id.linearLayout1) ;
//将adView添加到
layout.addView(adView);
//启动一个通用请求以加载广告
adView.loadAd(new AdRequest());}
@Override
public void onDestroy(){
adView.destroy();
super.onDestroy();
}}


现在,当我开始AdView中的应用程序是一个错误,称为您必须在AndroidManifest.xml中使用configChanges声明AdActivity。



我使用Android 4.0以上3.2以上,但不起作用。
我希望有人可以帮助我。

解决方案

我有完全相同的问题,得到你必须有AdActivity在AndroidManifest.xml中用configChanges声明。整合最新的AdMob SDK后出现错误讯息。尽管我在StackOverflow中发现了这两个相关的讨论(参见本文底部的链接),但是并没有帮助我解决这个问题,因为 - 对我来说 - 他们之间没有足够的区别清单中的 targetSdkVersion 属性和构建目标。这个答案描述了什么解决了我的问题,造成了麻烦。



解决方案



最简单的部分:您的 AdActivity 定义的 configChanges 属性中缺少几个标志>的AndroidManifest.xml 。如 AdMob SDK文档,定义需要如下所示:

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

第二个更复杂的部分与SDK目标相关:唯一的解决方案真的好像是使用SDK管理器安装SDK至少Android 3.2(API级别13)。安装此SDK版本后,您需要配置IDE以使用此SDK构建项目。确切的设置取决于您正在使用的IDE。在我的情况下,它是IntelliJ IDEA,您将在标题项目SDK下面的项目页面上的项目设置中找到该选项。



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

  target = android-13 

上面的配置是一个窍门。 AndroidManifest中的< uses-sdk> 元素需要更改更改。阅读下面的陷阱,了解为什么会引起麻烦。



说明



< uses-sdk> 元素具有完全不同的范围。



构建目标仅在构建时通过构建工具评估,以确定哪个版本的SDK工具您的系统应该用于构建您的应用程序。 SDK越新,API知识就越多。无论Google为什么强迫我们指定一些 configChanges ,在API级别13之前不可用,因此我们需要至少使用SDK工具13构建我们的应用程序,因为以前版本的SDK工具不知道这些新的 configChanges ,并会报告错误。在运行时,构建目标没有任何意义,Android将忽略它不知道的所有元素(例如 configChanges )。



相对于Android清单中的< uses-sdk> 元素指定的 targetSdkVersion 元素仅限于 / em>在运行时评估 - 不是在编译时。实际上,您可以在此处指定任何值,而编译器不需要更改任何内容或提出错误消息。这就是为什么要更改此属性不能帮助我们解决我们的AdMob问题。另一方面,在运行时,属性可以由android来评估,以支持一些针对旧版本的Android应用程序的兼容性功能。查看下面的陷阱部分,看看给我带来麻烦的主题。



陷阱




    如果您无法在此Android版本上测试您的应用程式,则
  • 不要将更改集 targetSdkVersion 更高版本因为我误解了关于这个admob主题的现有答案,我还设置了< uses-sdk>的 android:targetSdkVersion 属性。 元素到我的应用程序中的API级别13导致了致命的副作用:由于Android 3认为我的应用程序将本机支持蜂巢,它不再显示在底部边框的软按钮栏中的菜单按钮并且当我的应用隐藏本机标题栏以显示它自己的实现时,用户没有任何进一步访问蜂巢的菜单的可能性。所以对于我离开 targetSdkVersion 在级别 10 有助于恢复菜单按钮,并且工作正常。

  • 处理与SDK工具中可能丢失的旧API的向后兼容性13:确定,所以我已经将我的构建过程设置为使用SDK工具13和我的 targetSdkVersion 到10,一切都应该好,对吧?不幸的是!原因是,我的应用程序与Android 1.5(API 3级)兼容,因为这仍然占我用户的5%左右。不幸的是,在将构建目标设置为13之后,我的代码的某些部分没有编译,因为它们引用了不支持的方法,直到SDK工具10支持,但不再从SDK工具11开始(例如 Service.setForeground )。



如何处理向后兼容性的基础知识描述 here - 但是本文没有描述如何调用不再可用的方法,因为它们将导致编译器错误。我通过创建我的应用程序使用的新的库项目来解决这个问题。对于这个库项目,我将构建目标设置为10,这将导致它使用仍然知道我所使用的android已弃用的API的SDK工具10进行编译。我的应用程序然后从这个兼容性库调用帮助方法,所以我可以使用较新的目标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天全站免登陆