Question

I am new to Google AdMob. I have a PreferenceActivity that I want to put a banner inside. I add the code from that google provided at https://developers.google.com/mobile-ads-sdk/docs/admob/fundamentals#play. But I seem to get this error when I try to go to the settings portion when I run my app.

My code is as follows:

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();
      }  
}

Here is my Logcat error:

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

my custom layout 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>
Was it helpful?

Solution

Several problems here:

  1. PreferenceActitivty is way different to a standard actiovity. For one you can't use a layout such as you have tried to do. See http://developer.android.com/reference/android/preference/PreferenceActivity.html
  2. Don't put an add in a preference activity. Put ads where you user's spend most of their time.
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top