首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >无法启动活动ComponentInfo "AdMob“

无法启动活动ComponentInfo "AdMob“
EN

Stack Overflow用户
提问于 2014-03-13 11:31:02
回答 1查看 616关注 0票数 0

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

我的代码如下:

代码语言:javascript
复制
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错误:

代码语言:javascript
复制
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

代码语言:javascript
复制
<?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>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-14 04:12:56

这里有几个问题:

  1. PreferenceActitivty与标准的活动完全不同。首先,您不能像您曾经尝试过的那样使用布局。请参阅http://developer.android.com/reference/android/preference/PreferenceActivity.html
  2. 不要在偏好活动中添加一个加法。把广告放在你的用户花大部分时间的地方。
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22377264

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档