无法启动活动ComponentInfo“AdMob”

时间:2014-03-13 11:31:02

标签: android admob google-play-services preferenceactivity

我是Google AdMob的新手。我有一个PreferenceActivity,我想在里面放一个横幅。我添加了https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play提供的谷歌代码。但是当我在运行应用程序时尝试转到设置部分时,我似乎遇到了这个错误。

我的代码如下:

public class Pref extends PreferenceActivity implements
        SharedPreferences.OnSharedPreferenceChangeListener {

    /** The view to show the ad. */
      private AdView adView;

      /* Your ad unit id. Replace with your actual ad unit id. */
      private static final String AD_UNIT_ID = "MY KEY";


    @SuppressWarnings("deprecation")
    protected void onCreate(Bundle paramBundle) {
        super.onCreate(paramBundle);
        getPreferenceManager()
                .setSharedPreferencesName("com.gordondev.copycat");
        addPreferencesFromResource(2130903040);
        getPreferenceManager().getSharedPreferences()
                .registerOnSharedPreferenceChangeListener(this);

        // Create an ad.
        adView = new AdView(this);
        adView.setAdSize(AdSize.BANNER);
        adView.setAdUnitId(AD_UNIT_ID);

        // Add the AdView to the view hierarchy. The view will have no size
        // until the ad is loaded.
        LinearLayout layout = (LinearLayout) findViewById(R.id.ad_layout);
        layout.addView(adView);

        // Create an ad request. Check logcat output for the hashed device ID to
        // get test ads on a physical device.
        AdRequest adRequest = new AdRequest.Builder()
            .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
            .addTestDevice("INSERT_YOUR_HASHED_DEVICE_ID_HERE")
            .build();

        // Start loading the ad in the background.
        adView.loadAd(adRequest);


    }

    /** Called before the activity is destroyed. */
    @SuppressWarnings("deprecation")
    protected void onDestroy() {
        getPreferenceManager().getSharedPreferences()
                .unregisterOnSharedPreferenceChangeListener(this);
         if (adView != null) {
              adView.destroy();
            }
        super.onDestroy();
    }

    public void onSharedPreferenceChanged(
            SharedPreferences paramSharedPreferences, String paramString) {
    }

     @Override
      public void onResume() {
        super.onResume();
        if (adView != null) {
          adView.resume();
        }
      }

      @Override
      public void onPause() {
        if (adView != null) {
          adView.pause();
        }
        super.onPause();
      }  
}

这是我的Logcat错误:

03-13 07:21:26.192: E/AndroidRuntime(25248): FATAL EXCEPTION: main
03-13 07:21:26.192: E/AndroidRuntime(25248): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gordondev.copycat/com.gordondev.copycat.Pref}: android.view.InflateException: Binary XML file line #2: Error inflating class LinearLayout
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2266)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2316)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread.access$600(ActivityThread.java:150)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1298)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.os.Looper.loop(Looper.java:213)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread.main(ActivityThread.java:5225)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at java.lang.reflect.Method.invokeNative(Native Method)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at java.lang.reflect.Method.invoke(Method.java:525)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:786)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:602)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at dalvik.system.NativeStart.main(Native Method)
03-13 07:21:26.192: E/AndroidRuntime(25248): Caused by: android.view.InflateException: Binary XML file line #2: Error inflating class LinearLayout
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.inflate(GenericInflater.java:317)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.inflate(GenericInflater.java:263)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1439)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at com.gordondev.copycat.Pref.onCreate(Pref.java:27)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.Activity.performCreate(Activity.java:5133)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1087)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2230)
03-13 07:21:26.192: E/AndroidRuntime(25248):    ... 11 more
03-13 07:21:26.192: E/AndroidRuntime(25248): Caused by: java.lang.ClassNotFoundException: Didn't find class "android.preference.LinearLayout" on path: DexPathList[[zip file "/data/app/com.gordondev.copycat-1.apk"],nativeLibraryDirectories=[/data/app-lib/com.gordondev.copycat-1, /vendor/lib, /system/lib]]
03-13 07:21:26.192: E/AndroidRuntime(25248):    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:53)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.createItem(GenericInflater.java:375)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
03-13 07:21:26.192: E/AndroidRuntime(25248):    at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
03-13 07:21:26.192: E/AndroidRuntime(25248):    ... 19 more

我的自定义布局ad_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:id="@+id/ad_layout"
    android:layout_height="match_parent" android:orientation="vertical">  
</LinearLayout>

1 个答案:

答案 0 :(得分:1)

这里有几个问题:

  1. PreferenceActitivty与标准的actiovity不同。例如,您不能使用您尝试过的布局。请参阅http://developer.android.com/reference/android/preference/PreferenceActivity.html
  2. 不要添加偏好活动。将广告投放到用户花费大部分时间的地方。