在Android应用中添加广告时会出现什么问题?

时间:2014-08-27 18:52:29

标签: android admob ads advertisement adview

我想在我的机器人中添加广告, 但是当我添加代码时,我有问题,因为错误消息。 你能帮我吗? 提前致谢 我想在我的机器人中添加广告, 但是当我添加代码时,我有问题,因为错误消息。 你能帮我吗? 提前致谢

MainActivity

adView = new AdView(this); adView.setAdSize(AdSize.BANNER); adView.setAdUnitId(AD_UNIT_ID);
LinearLayout layout = (LinearLayout) findViewById(R.id.linearLayout); layout.addView(adView);
AdRequest adRequest = new AdRequest.Builder() .addTestDevice(AdRequest.DEVICE_ID_EMULATOR) //.addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE") .build();
adView.loadAd(adRequest);

mainlayout

` <?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"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/linearLayout"
    android:orientation="horizontal"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.googleplay.adstest.MainActivity" >
<com.google.ads.AdView
       android:id="@+id/adView"
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:layout_alignParentBottom="true"
       ads:adSize="SMART_BANNER"
       ads:adUnitId="ca-app-pub-8841523261682278/8262798142"
       ads:loadAdOnCreate="true"
       ads:testDevices="TEST_EMULATOR"/>
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />

</LinearLayout>

` 清单

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.googleplay.adstest"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="14" />
<meta-data android:name="com.google.android.gms.version"
               android:value="@integer/google_play_services_version"/>
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name="com.google.android.gms.ads.AdActivity"
             android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"/>
    </application>
    <uses-permission android:name="android.permission.INTERNET"/>
  <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

</manifest>

logcat的

08-27 22:31:04.657: D/AndroidRuntime(22155): Shutting down VM
08-27 22:31:04.657: W/dalvikvm(22155): threadid=1: thread exiting with uncaught exception (group=0x41af6930)
08-27 22:31:04.697: E/AndroidRuntime(22155): FATAL EXCEPTION: main
08-27 22:31:04.697: E/AndroidRuntime(22155): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.googleplay.adstest/com.googleplay.adstest.MainActivity}: android.view.InflateException: Binary XML file line #15: Error inflating class com.google.ads.AdView
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.os.Looper.loop(Looper.java:176)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread.main(ActivityThread.java:5317)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at java.lang.reflect.Method.invoke(Method.java:511)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at dalvik.system.NativeStart.main(Native Method)
08-27 22:31:04.697: E/AndroidRuntime(22155): Caused by: android.view.InflateException: Binary XML file line #15: Error inflating class com.google.ads.AdView
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:704)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.Activity.setContentView(Activity.java:1932)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at com.googleplay.adstest.MainActivity.onCreate(MainActivity.java:17)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.Activity.performCreate(Activity.java:5326)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
08-27 22:31:04.697: E/AndroidRuntime(22155):    ... 11 more
08-27 22:31:04.697: E/AndroidRuntime(22155): Caused by: java.lang.ClassNotFoundException: Didn't find class "com.google.ads.AdView" on path: /data/app/com.googleplay.adstest-11.apk
08-27 22:31:04.697: E/AndroidRuntime(22155):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:65)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.createView(LayoutInflater.java:558)
08-27 22:31:04.697: E/AndroidRuntime(22155):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
08-27 22:31:04.697: E/AndroidRuntime(22155):    ... 24 more

编辑代码后的logcat

    08-27 22:40:10.499: W/dalvikvm(22789): VFY: unable to resolve static field 3541 (AdsAttrs) in Lcom/google/android/gms/R$styleable;
08-27 22:40:10.499: D/dalvikvm(22789): VFY: replacing opcode 0x62 at 0x0009
08-27 22:40:10.519: D/AndroidRuntime(22789): Shutting down VM
08-27 22:40:10.519: W/dalvikvm(22789): threadid=1: thread exiting with uncaught exception (group=0x41af6930)
08-27 22:40:10.539: E/AndroidRuntime(22789): FATAL EXCEPTION: main
08-27 22:40:10.539: E/AndroidRuntime(22789): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.advertexamole/com.advertexamole.MainActivity}: android.view.InflateException: Binary XML file line #10: Error inflating class com.google.android.gms.ads.AdView
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2255)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2309)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread.access$700(ActivityThread.java:157)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1289)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.os.Handler.dispatchMessage(Handler.java:99)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.os.Looper.loop(Looper.java:176)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread.main(ActivityThread.java:5317)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at java.lang.reflect.Method.invokeNative(Native Method)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at java.lang.reflect.Method.invoke(Method.java:511)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at dalvik.system.NativeStart.main(Native Method)
08-27 22:40:10.539: E/AndroidRuntime(22789): Caused by: android.view.InflateException: Binary XML file line #10: Error inflating class com.google.android.gms.ads.AdView
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.createView(LayoutInflater.java:619)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:693)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.rInflate(LayoutInflater.java:752)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.inflate(LayoutInflater.java:495)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.inflate(LayoutInflater.java:397)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.inflate(LayoutInflater.java:353)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:360)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.Activity.setContentView(Activity.java:1932)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.support.v7.app.ActionBarActivity.superSetContentView(ActionBarActivity.java:216)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.support.v7.app.ActionBarActivityDelegateICS.setContentView(ActionBarActivityDelegateICS.java:111)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:76)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.advertexamole.MainActivity.onCreate(MainActivity.java:16)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.Activity.performCreate(Activity.java:5326)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1097)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2218)
08-27 22:40:10.539: E/AndroidRuntime(22789):    ... 11 more
08-27 22:40:10.539: E/AndroidRuntime(22789): Caused by: java.lang.reflect.InvocationTargetException
08-27 22:40:10.539: E/AndroidRuntime(22789):    at java.lang.reflect.Constructor.constructNative(Native Method)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at android.view.LayoutInflater.createView(LayoutInflater.java:593)
08-27 22:40:10.539: E/AndroidRuntime(22789):    ... 25 more
08-27 22:40:10.539: E/AndroidRuntime(22789): Caused by: java.lang.NoClassDefFoundError: com.google.android.gms.R$styleable
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.google.android.gms.internal.ao.<init>(Unknown Source)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.google.android.gms.internal.au.<init>(Unknown Source)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.google.android.gms.internal.au.<init>(Unknown Source)
08-27 22:40:10.539: E/AndroidRuntime(22789):    at com.google.android.gms.ads.AdView.<init>(Unknown Source)
08-27 22:40:10.539: E/AndroidRuntime(22789):    ... 28 more

1 个答案:

答案 0 :(得分:0)

如果您在布局中执行ads:loadAdOnCreate="true"而在adView.loadAd(adRequest);中不应该执行onCreate()。 但是从onCreate()方法加载adView会更好。

工作代码:

Inside OnCreate:

AdView adView = (AdView) this.findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);

<强> 的AdView:

<LinearLayout
        android:id="@+id/text_layout"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        xmlns:ads="http://schemas.android.com/apk/res-auto"
        android:orientation="vertical" 
        xmlns:android="http://schemas.android.com/apk/res/android">
        <com.google.android.gms.ads.AdView 
             android:id="@+id/adView"
             android:layout_width="fill_parent"
             android:layout_height="wrap_content"
             android:background="#FFFFFF"
             ads:adUnitId="PUT YOUR ID"
             ads:adSize="BANNER"/>
</LinearLayout>

提示: 并确保添加了Play商店库。在你的项目中。