我正在使用这段代码,它看起来很完美,没有任何错误,但不知道为什么我的应用程序每次运行我的应用程序都会崩溃..
<com.google.ads.AdView
android:id="@+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId= "idthatigotthroughadmob"
ads:loadAdOnCreate="true"
ads:adSize="BANNER"
/>
在android的manifest.xml中
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
在
<application><activity
android:name="com.google.ads.AdActivity"
android:configChanges="keyboard|keyboardHidden|orientation"
></activity> </application>
logcat 显示
04-12 21:30:09.655: E/dalvikvm(272): Could not find class 'com.google.ads.AdView',
referenced from method com.project.hisaabkikitaab.MainActivity.onCreate
04-12 21:30:09.655: W/dalvikvm(272): VFY: unable to resolve check-cast 471 (Lcom/google
/ads/AdView;) in Lcom/project/hisaabkikitaab/MainActivity;
04-12 21:30:09.655: D/dalvikvm(272): VFY: replacing opcode 0x1f at 0x000e
04-12 21:30:09.655: D/dalvikvm(272): VFY: dead code 0x0010-02b9 in Lcom/project
/hisaabkikitaab/MainActivity;.onCreate (Landroid/os/Bundle;)V
04-12 21:30:09.815: D/AndroidRuntime(272): Shutting down VM
04-12 21:30:09.815: W/dalvikvm(272): threadid=1: thread exiting with uncaught
exception (group=0x4001d800)
04-12 21:30:09.845: E/AndroidRuntime(272): FATAL EXCEPTION: main
04-12 21:30:09.845: E/AndroidRuntime(272): java.lang.NoClassDefFoundError:
com.google.ads.AdView
04-12 21:30:09.845: E/AndroidRuntime(272): at
com.project.hisaabkikitaab.MainActivity.onCreate(MainActivity.java:42)
04-12 21:30:09.845: E/AndroidRuntime(272): at
.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
最佳答案
您还需要将 ads
命名空间添加到布局根节点
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
您可能从中获取示例的教程已经包含了它:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.google.ads.AdView android:id="@+id/adView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
ads:adUnitId="MY_AD_UNIT_ID"
ads:adSize="BANNER"
ads:testDevices="TEST_EMULATOR, TEST_DEVICE_ID"
ads:loadAdOnCreate="true"/>
</LinearLayout>
来源:https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals , Android 选项卡
另一种可能的解决方案:
您的项目中有libs
文件夹吗?如果是的话,是否有 admob
库?如果您对其中任何一个拒绝,请确保创建一个 libs
文件夹(与 res
和 src
处于同一级别,然后复制您的 admob
库。清理项目并重建。
关于android - 如何添加谷歌AdSense?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15977089/