当我试图在我的应用程序中获取admob时,我的应用程序会强制关闭

时间:2012-06-27 01:00:19

标签: android

您好我是第一次尝试为我的应用添加admob。我已经阅读了很多信息(包括stackOverFlow)。但是我还没有运气。我的应用程序关闭力量。 这是我到目前为止的尝试方式。

的AndroidManifest.xml

我在应用程序标记

中添加了这些行
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

我还在

之前添加了以下行
<Meta-Data android:value="a14f9xxxxx(id from admob)" android:name="ADMOB_PUBLISHER_ID"/>

attr.xml

<?xml version="1.0" encoding="utf-8"?>
  <resources>
    <declare-styleable name="com.admob.android.ads.AdView">            
       <attr name="backgroundColor" format="color" />
       <attr name="primaryTextColor" format="color" />
       <attr name="secondaryTextColor" format="color" />
       <attr name="keywords" format="string" />
       <attr name="refreshInterval" format="integer" />
    </declare-styleable>
  </resources>

frontpage.xml(我的首页活动的布局

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:myapp="http://schemas.android.com/apk/res/simple.stocktaketrial"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical" >
...
some other layout and components here
...

<com.admob.android.ads.AdView     
       android:id="@+id/ad" 
       android:layout_width="fill_parent" 
       android:layout_height="wrap_content"
       myapp:backgroundColor="#000000"
       myapp:primaryTextColor="#FFFFFF"
       myapp:secondaryTextColor="#CCCCCC"
/>
</LinearLayout>

我在lib文件夹中添加了GoogleAdMobAdsSdk-6.0.0并构建了传递。

我做错了什么?

我已经阅读过在布局中在xml中添加admob的方法比向java添加代码要好。所以,如果确实如此,我想保留它。

先谢谢你的帮助。

这里是logcat输出。

06-27 12:24:56.630: E/AndroidRuntime(25265): FATAL EXCEPTION: main
06-27 12:24:56.630: E/AndroidRuntime(25265): java.lang.RuntimeException: Unable to start activity ComponentInfo{simple.stocktaketrial/simple.stocktaketrial.FrontPage}: android.view.InflateException: Binary XML file line #77: Error inflating class com.admob.android.ads.AdView
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread.access$2300(ActivityThread.java:125)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.os.Handler.dispatchMessage(Handler.java:99)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.os.Looper.loop(Looper.java:123)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread.main(ActivityThread.java:4627)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at java.lang.reflect.Method.invokeNative(Native Method)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at java.lang.reflect.Method.invoke(Method.java:521)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at dalvik.system.NativeStart.main(Native Method)
06-27 12:24:56.630: E/AndroidRuntime(25265): Caused by: android.view.InflateException: Binary XML file line #77: Error inflating class com.admob.android.ads.AdView
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:576)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:618)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.inflate(LayoutInflater.java:407)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.inflate(LayoutInflater.java:320)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.inflate(LayoutInflater.java:276)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:198)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.Activity.setContentView(Activity.java:1647)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at simple.stocktaketrial.FrontPage.onCreate(FrontPage.java:25)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
06-27 12:24:56.630: E/AndroidRuntime(25265):    ... 11 more
06-27 12:24:56.630: E/AndroidRuntime(25265): Caused by: java.lang.ClassNotFoundException: com.admob.android.ads.AdView in loader dalvik.system.PathClassLoader[/data/app/simple.stocktaketrial-2.apk]
06-27 12:24:56.630: E/AndroidRuntime(25265):    at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
06-27 12:24:56.630: E/AndroidRuntime(25265):    at android.view.LayoutInflater.createView(LayoutInflater.java:466)
06-2 7 12:24:56.630: E/AndroidRuntime(25265):   at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:565)
06-27 12:24:56.630: E/AndroidRuntime(25265):    ... 20 more

1 个答案:

答案 0 :(得分:0)

看起来你错过了AndroidManifest.xml中的活动以及'Meta-Data'的错误案例......

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="...." android:versionCode=".." android:versionName="...">
   <application android:icon="...." android:label="....">
      <activity ..... >
      </activity>
      <meta-data android:value="a14f9xxxxx(id from admob)" android:name="ADMOB_PUBLISHER_ID"/>
      <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
   </application>
</manifest>

以上configChanges属性适用于Android SDK等级14及以上版本。

对于较低版本,请使用:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
       package="...." android:versionCode=".." android:versionName="...">
   <application android:icon="...." android:label="....">
      <activity ..... >
      </activity>
      <meta-data android:value="a14f9xxxxx(id from admob)" android:name="ADMOB_PUBLISHER_ID"/>
      <activity android:name="com.google.ads.AdActivity" android:configChanges="keyboard|keyboardHidden|orientation"/>
    </application>
</manifest>

Admobs库中的活动,无需额外做任何事情:)