AdMob Activity Android出错

时间:2014-07-01 17:24:03

标签: android admob

我是初学者,我正在为Android实施Admob但却出错了。请帮我解决。

// Using Google_play_Services_lib

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;

public class MainActivity extends Activity {
    AdView adView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
         adView=(AdView)findViewById(R.id.MyAdView);
         AdRequest adRequest=new AdRequest.Builder()
         .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
         .addTestDevice("MDI381SO315FMD83").build();
         adView.loadAd(adRequest);
    }

    @Override
     public void onPause() {
     adView.pause();
     super.onPause();
     }

    @Override
     public void onResume() {
     super.onResume();
     adView.resume();
     }

    @Override
     public void onDestroy() {
     adView.destroy();
     super.onDestroy();
     }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

}

ACTIVITY_MAIN XML文件:

<?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="vertical"
              android:layout_width="match_parent"
              android:layout_height="match_parent">

  <com.google.android.gms.ads.AdView
        android:id="@+id/MyAdView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        ads:adSize="SMART_BANNER"
        ads:adUnitId="21XXXXXXXXXXXX" />
</LinearLayout>

Logcat(我不知道错误发生在哪里):

07-01 20:16:43.456: D/dalvikvm(537): DexOpt: couldn't find static field Lcom/google/android/gms/R$styleable;.AdsAttrs
07-01 20:16:43.456: W/dalvikvm(537): VFY: unable to resolve static field 1488 (AdsAttrs) in Lcom/google/android/gms/R$styleable;
07-01 20:16:43.465: D/dalvikvm(537): VFY: replacing opcode 0x62 at 0x0009
07-01 20:16:43.485: D/AndroidRuntime(537): Shutting down VM
07-01 20:16:43.485: W/dalvikvm(537): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-01 20:16:43.566: E/AndroidRuntime(537): FATAL EXCEPTION: main
07-01 20:16:43.566: E/AndroidRuntime(537): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andro.admob/com.andro.admob.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.gms.ads.AdView
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.os.Looper.loop(Looper.java:137)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread.main(ActivityThread.java:4340)
07-01 20:16:43.566: E/AndroidRuntime(537):  at java.lang.reflect.Method.invokeNative(Native Method)
07-01 20:16:43.566: E/AndroidRuntime(537):  at java.lang.reflect.Method.invoke(Method.java:511)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-01 20:16:43.566: E/AndroidRuntime(537):  at dalvik.system.NativeStart.main(Native Method)
07-01 20:16:43.566: E/AndroidRuntime(537): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.gms.ads.AdView
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.createView(LayoutInflater.java:606)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.Activity.setContentView(Activity.java:1835)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.andro.admob.MainActivity.onCreate(MainActivity.java:16)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.Activity.performCreate(Activity.java:4465)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-01 20:16:43.566: E/AndroidRuntime(537):  ... 11 more
07-01 20:16:43.566: E/AndroidRuntime(537): Caused by: java.lang.reflect.InvocationTargetException
07-01 20:16:43.566: E/AndroidRuntime(537):  at java.lang.reflect.Constructor.constructNative(Native Method)
07-01 20:16:43.566: E/AndroidRuntime(537):  at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-01 20:16:43.566: E/AndroidRuntime(537):  at android.view.LayoutInflater.createView(LayoutInflater.java:586)
07-01 20:16:43.566: E/AndroidRuntime(537):  ... 22 more
07-01 20:16:43.566: E/AndroidRuntime(537): Caused by: java.lang.NoSuchFieldError: com.google.android.gms.R$styleable.AdsAttrs
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.google.android.gms.internal.an.<init>(Unknown Source)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.google.android.gms.internal.at.<init>(Unknown Source)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.google.android.gms.internal.at.<init>(Unknown Source)
07-01 20:16:43.566: E/AndroidRuntime(537):  at com.google.android.gms.ads.AdView.<init>(Unknown Source)
07-01 20:16:43.566: E/AndroidRuntime(537):  ... 25 more
07-01 22:25:46.036: D/dalvikvm(1026): DexOpt: couldn't find static field Lcom/google/android/gms/R$styleable;.AdsAttrs
07-01 22:25:46.036: W/dalvikvm(1026): VFY: unable to resolve static field 1488 (AdsAttrs) in Lcom/google/android/gms/R$styleable;
07-01 22:25:46.036: D/dalvikvm(1026): VFY: replacing opcode 0x62 at 0x0009
07-01 22:25:46.056: D/AndroidRuntime(1026): Shutting down VM
07-01 22:25:46.056: W/dalvikvm(1026): threadid=1: thread exiting with uncaught exception (group=0x409961f8)
07-01 22:25:46.066: E/AndroidRuntime(1026): FATAL EXCEPTION: main
07-01 22:25:46.066: E/AndroidRuntime(1026): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.andro.admob/com.andro.admob.MainActivity}: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.gms.ads.AdView
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1955)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1980)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread.access$600(ActivityThread.java:122)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1146)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.os.Looper.loop(Looper.java:137)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread.main(ActivityThread.java:4340)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invokeNative(Native Method)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at java.lang.reflect.Method.invoke(Method.java:511)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at dalvik.system.NativeStart.main(Native Method)
07-01 22:25:46.066: E/AndroidRuntime(1026): Caused by: android.view.InflateException: Binary XML file line #12: Error inflating class com.google.android.gms.ads.AdView
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.createView(LayoutInflater.java:606)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:680)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.rInflate(LayoutInflater.java:739)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.inflate(LayoutInflater.java:489)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.inflate(LayoutInflater.java:352)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.android.internal.policy.impl.PhoneWindow.setContentView(PhoneWindow.java:251)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.Activity.setContentView(Activity.java:1835)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.andro.admob.MainActivity.onCreate(MainActivity.java:16)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.Activity.performCreate(Activity.java:4465)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1049)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1919)
07-01 22:25:46.066: E/AndroidRuntime(1026):     ... 11 more
07-01 22:25:46.066: E/AndroidRuntime(1026): Caused by: java.lang.reflect.InvocationTargetException
07-01 22:25:46.066: E/AndroidRuntime(1026):     at java.lang.reflect.Constructor.constructNative(Native Method)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at java.lang.reflect.Constructor.newInstance(Constructor.java:417)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at android.view.LayoutInflater.createView(LayoutInflater.java:586)
07-01 22:25:46.066: E/AndroidRuntime(1026):     ... 22 more
07-01 22:25:46.066: E/AndroidRuntime(1026): Caused by: java.lang.NoSuchFieldError: com.google.android.gms.R$styleable.AdsAttrs
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.google.android.gms.internal.an.<init>(Unknown Source)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.google.android.gms.internal.at.<init>(Unknown Source)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.google.android.gms.internal.at.<init>(Unknown Source)
07-01 22:25:46.066: E/AndroidRuntime(1026):     at com.google.android.gms.ads.AdView.<init>(Unknown Source)
07-01 22:25:46.066: E/AndroidRuntime(1026):     ... 25 more

1 个答案:

答案 0 :(得分:-2)

尝试更改此行:

adView=(AdView)findViewById(R.id.MyAdView);

为:

AdView adView = (AdView) this.findViewById(R.id.MyAdView);

或者:确保设备具有Google Play服务APK。 Android模拟器不支持Google Play服务 - 要使用API​​进行开发,您需要提供Android手机或平板电脑等开发设备。

To verify, call isGooglePlayServicesAvailable(). Check the link:

http://developer.android.com/google/play-services/setup.html#ensure