title: Error inflating layout with java.lang.ClassNotFoundException
body: <p>OK now I know that this question has been asked before several times on SO but none of the answers have worked for me.</p>
  
  <p>I am attempting to create a custom preference for my project. More specifically it is a preference with a <a href="http://www.dev-smart.com/archives/34" rel="nofollow"><code>HorizontalListView</code></a> attached directly underneath it. I basically created it by modifying <a href="http://robobunny.com/wp/2011/08/13/android-seekbar-preference/" rel="nofollow">this code</a> for a <code>SeekBarPreference</code> (which I am also using and is working fine). My <code>ListViewPreference</code> is located in exactly the same folder as the <code>SeelkBarPreference</code> (which, as I said is having no problem) but I am constantly getting a <code>ClassNotFoundException</code> (see logcat below). Here is my <code>ListViewPreference</code> class:</p>
  
  <pre><code>package com.example.ebookreader;
  
  import android.content.Context;
  import android.content.res.TypedArray;
  import android.preference.Preference;
  import android.util.AttributeSet;
  import android.util.Log;
  import android.view.LayoutInflater;
  import android.view.View;
  import android.view.ViewGroup;
  import android.view.ViewGroup.LayoutParams;
  import android.view.ViewParent;
  import android.widget.RelativeLayout;
  import android.widget.TextView;
  
  public class ListViewPreference extends Preference {
  
      private final String TAG = getClass().getName();
  
      private static final String ROBOBUNNYNS = "http://robobunny.com";
      private static final int DEFAULT_VALUE = 50;
  
      private int mCurrentValue;
      private String mUnitsLeft = "";
      private String mUnitsRight = "";
  
      private HorizontalListView mListView;
  
      private TextView mStatusText;
  
      public ListViewPreference(Context context, AttributeSet attrs) {
          super(context, attrs);
          initPreference(context, attrs);
      }
  
      public ListViewPreference(Context context, AttributeSet attrs, int defStyle) {
          super(context, attrs, defStyle);
          initPreference(context, attrs);
      }
  
      private void initPreference(Context context, AttributeSet attrs) {
          setValuesFromXml(attrs);
  
          mListView = new HorizontalListView(context, attrs);
          LayoutParams params = mListView.getLayoutParams();
          params.width = LayoutParams.MATCH_PARENT;
          params.height = LayoutParams.WRAP_CONTENT;
          mListView.setLayoutParams(params);
      }
  
      private void setValuesFromXml(AttributeSet attrs) {
          mUnitsLeft = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsLeft",
                  "");
          String units = getAttributeStringValue(attrs, ROBOBUNNYNS, "units", "");
          mUnitsRight = getAttributeStringValue(attrs, ROBOBUNNYNS, "unitsRight",
                  units);
      }
  
      private String getAttributeStringValue(AttributeSet attrs,
              String namespace, String name, String defaultValue) {
          String value = attrs.getAttributeValue(namespace, name);
  
          if (value == null)
              value = defaultValue;
  
          return value;
      }
  
      @Override
      protected View onCreateView(ViewGroup parent) {
          RelativeLayout layout = null;
  
          try {
              LayoutInflater mInflater = (LayoutInflater) getContext()
                      .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
  
              layout = (RelativeLayout) mInflater.inflate(
                      R.layout.horizontal_list_view_preference, parent, false);
          } catch (Exception e) {
              Log.e(TAG, "Error creating seek bar preference", e);
          }
  
          return layout;
      }
  
      @Override
      public void onBindView(View view) {
          super.onBindView(view);
  
          try {
              // move our seekbar to the new view we've been given
              ViewParent oldContainer = mListView.getParent();
              ViewGroup newContainer = (ViewGroup) view
                      .findViewById(R.id.listViewPrefBarContainer);
  
              if (oldContainer != newContainer) {
                  // remove the seekbar from the old view
                  if (oldContainer != null) {
                      ((ViewGroup) oldContainer).removeView(mListView);
                  }
                  // remove the existing seekbar (there may not be one) and add
                  // ours
                  newContainer.removeAllViews();
                  newContainer.addView(mListView,
                          ViewGroup.LayoutParams.FILL_PARENT,
                          ViewGroup.LayoutParams.WRAP_CONTENT);
              }
          } catch (Exception ex) {
              Log.e(TAG, "Error binding view: " + ex.toString());
          }
  
          updateView(view);
      }
  
      /**
      * Update a SeekBarPreference view with our current state
      * 
      * @param view
      */
      protected void updateView(View view) {
          try {
              RelativeLayout layout = (RelativeLayout) view;
  
              mStatusText = (TextView) layout
                      .findViewById(R.id.listViewPrefValue);
              mStatusText.setText(String.valueOf(mCurrentValue));
              mStatusText.setMinimumWidth(30);
  
              TextView unitsRight = (TextView) layout
                      .findViewById(R.id.listViewPrefUnitsRight);
              unitsRight.setText(mUnitsRight);
  
              TextView unitsLeft = (TextView) layout
                      .findViewById(R.id.listViewPrefUnitsLeft);
              unitsLeft.setText(mUnitsLeft);
          } catch (Exception e) {
              Log.e(TAG, "Error updating seek bar preference", e);
          }
      }
  
      @Override
      protected Object onGetDefaultValue(TypedArray ta, int index) {
          int defaultValue = ta.getInt(index, DEFAULT_VALUE);
          return defaultValue;
      }
  
      @Override
      protected void onSetInitialValue(boolean restoreValue, Object defaultValue) {
          if (restoreValue) {
              mCurrentValue = getPersistedInt(mCurrentValue);
          } else {
              int temp = 0;
              try {
                  temp = (Integer) defaultValue;
              } catch (Exception ex) {
                  Log.e(TAG, "Invalid default value: " + defaultValue.toString());
              }
  
              persistInt(temp);
              mCurrentValue = temp;
          }
      }
  }
  </code></pre>
  
  <p>For most people this problem is the result of not having a constructor with parameters <code>Context</code> and <code>AttributeSet</code> but as you can see, I clearly have it.</p>
  
  <p>Here is the XML file where the error keeps on occurring:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
      xmlns:custom="http://schemas.android.com/apk/res/com.example.ebookreader"
      android:background="@drawable/wallpaper" &gt;
  
      &lt;android:PreferenceCategory
          android:key="device_settings"
          android:title="Device Settings" &gt;
  
          &lt;android:CheckBoxPreference
              android:key="wifi"
              android:summary="Enable or Disable Wi-Fi"
              android:title="Wi-Fi" /&gt;
  
          &lt;android:CheckBoxPreference
              android:key="bluetooth"
              android:summary="Enable or Disable Bluetooth"
              android:title="Bluetooth" /&gt;
  
          &lt;android:CheckBoxPreference
              android:key="autosync"
              android:summary="Enable or Disable AutoSync"
              android:title="AutoSync" /&gt;
  
          &lt;custom:SeekBarPreference
              android:id="@+id/brightness_adjust"
              android:defaultValue="100"
              android:key="brightness"
              android:max="200"
              android:summary="Adjust Brightness Levels"
              android:title="Brightness" /&gt;
      &lt;/android:PreferenceCategory&gt;
  
      &lt;android:PreferenceCategory
          android:key="account_settings"
          android:title="Account Settings" &gt;
  
          &lt;custom:ListViewPreference&gt; &lt;!-- Error happens here --&gt;
                  android:id="@+id/font_selector"
                  android:key="font"
                  android:title="Font"
                  android:summary="Edit Font"     
              /&gt;
          &lt;/custom:ListViewPreference&gt;
      &lt;/android:PreferenceCategory&gt;
  
  &lt;/PreferenceScreen&gt;
  </code></pre>
  
  <p>Below is my full Logcat:</p>
  
  <pre><code>03-14 17:53:14.290: E/AndroidRuntime(449): FATAL EXCEPTION: main
  03-14 17:53:14.290: E/AndroidRuntime(449): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.ebookreader/com.example.ebookreader.SettingsActivity}: android.view.InflateException: Binary XML file line #40: Error inflating class ListViewPreference
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1736)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1752)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread.access$1500(ActivityThread.java:123)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:993)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.os.Handler.dispatchMessage(Handler.java:99)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.os.Looper.loop(Looper.java:126)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread.main(ActivityThread.java:3997)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at java.lang.reflect.Method.invokeNative(Native Method)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at java.lang.reflect.Method.invoke(Method.java:491)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:841)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at dalvik.system.NativeStart.main(Native Method)
  03-14 17:53:14.290: E/AndroidRuntime(449): Caused by: android.view.InflateException: Binary XML file line #40: Error inflating class ListViewPreference
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:441)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.inflate(GenericInflater.java:326)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.inflate(GenericInflater.java:263)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:269)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.PreferenceActivity.addPreferencesFromResource(PreferenceActivity.java:1333)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at com.example.ebookreader.SettingsActivity.onCreate(SettingsActivity.java:31)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1700)
  03-14 17:53:14.290: E/AndroidRuntime(449):  ... 11 more
  03-14 17:53:14.290: E/AndroidRuntime(449): Caused by: java.lang.ClassNotFoundException: android.preference.ListViewPreference in loader dalvik.system.PathClassLoader[/data/app/com.example.ebookreader-2.apk]
  03-14 17:53:14.290: E/AndroidRuntime(449):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:251)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at java.lang.ClassLoader.loadClass(ClassLoader.java:548)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at java.lang.ClassLoader.loadClass(ClassLoader.java:508)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.createItem(GenericInflater.java:375)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
  03-14 17:53:14.290: E/AndroidRuntime(449):  at    android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
  03-14 17:53:14.290: E/AndroidRuntime(449):  ... 20 more
  </code></pre>
  
  <p>Any help would be greatly appreciated.</p>