Android: Error inflating class PreferenceScreen

tarja picture tarja · Apr 5, 2014 · Viewed 10.4k times · Source

Please help me out here, I'm hitting brick wall for the last two days and can't figure it out.

I have a simple PreferenceFragment and would like to inflate it from an Activity.

Here is the res/xml/preferences.xml:

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android"
    android:key="preferences">
<PreferenceCategory
        android:title="@string/notification_basic"
        android:key="notification">
    <CheckBoxPreference
            android:key="enable_notification"
            android:summary="@string/enable_notification"
            android:title="@string/notification_title"
            android:defaultValue="false">
    </CheckBoxPreference>
    <CheckBoxPreference
            android:key="enable"
            android:summary="@string/enable_instant_notification"
            android:title="@string/notify_me_instantly"
            android:dependency="enable_notification"
            android:defaultValue="false">
    </CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_where_section"
        android:key="locations">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_where_title"
            android:key="location"
            android:summary="@string/sign_up_notify_me_where"
            android:title="@string/sign_up_notify_me_where_title"
            android:entries="@array/locations"
            android:entryValues="@array/locations_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_type_section"
        android:key="types">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_type_title"
            android:key="type"
            android:summary="@string/sign_up_notify_me_type"
            android:title="@string/sign_up_notify_me_type_title"
            android:entries="@array/types"
            android:entryValues="@array/types_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
</PreferenceCategory>
<PreferenceCategory
        android:title="@string/sign_up_notify_me_who_section"
        android:key="units">
    <MultiSelectListPreference
            android:dialogTitle="@string/sign_up_notify_me_who_title"
            android:key="units"
            android:summary="@string/sign_up_notify_me_who"
            android:title="@string/sign_up_notify_me_who_title"
            android:entries="@array/units"
            android:entryValues="@array/units_values"
            android:defaultValue="@array/empty_array"
            android:dependency="enable_notification"
            />
    </PreferenceCategory>
</PreferenceScreen>

The Activity:

public class HomeActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.home);
    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.add(R.id.fragment_container, new SignUpPreferenceFragment());
    fragmentTransaction.commit();
    }
}

The layout of the activity:

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:id="@+id/home_layout">
<FrameLayout android:layout_height="wrap_content"
             android:layout_width="wrap_content"
             android:id="@+id/fragment_container"
        />
</RelativeLayout>

And finally the stacktrace:

04-05 20:11:07.641: ERROR/AndroidRuntime(5731): FATAL EXCEPTION: main
    java.lang.RuntimeException: Unable to start activity     ComponentInfo{com.soid.client.activities/com.soid.client.activities.HomeActivity}:     android.view.InflateException: Binary XML file line #1: Error inflating class     PreferenceScreen
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2355)
    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2391)
    at android.app.ActivityThread.access$600(ActivityThread.java:151)
    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1335)
    at android.os.Handler.dispatchMessage(Handler.java:99)
    at android.os.Looper.loop(Looper.java:155)
    at android.app.ActivityThread.main(ActivityThread.java:5520)
    at java.lang.reflect.Method.invokeNative(Native Method)
    at java.lang.reflect.Method.invoke(Method.java:511)
    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1029)
    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:796)
    at dalvik.system.NativeStart.main(Native Method)
    Caused by: android.view.InflateException: Binary XML file line #1: Error inflating     class PreferenceScreen
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:698)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:466)
    at android.view.LayoutInflater.inflate(LayoutInflater.java:396)
    at     com.soid.client.activities.SignUpPreferenceFragment.onCreateView(SignUpPreferenceFragment.ja    va:18)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:831)
    at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1037)
    at android.app.BackStackRecord.run(BackStackRecord.java:635)
    at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1399)
    at android.app.Activity.performStart(Activity.java:5075)
    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2328)
    ... 11 more
    Caused by: java.lang.ClassNotFoundException: android.view.PreferenceScreen
    at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:501)
    at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
    at android.view.LayoutInflater.createView(LayoutInflater.java:552)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:643)
    at    com.android.internal.policy.impl.PhoneLayoutInflater.onCreateView(PhoneLayoutInflater.java:66)
    at android.view.LayoutInflater.onCreateView(LayoutInflater.java:660)
    at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:685)
    ... 20 more

Why does it search for the PreferenceScreen in android.view? I'm totally lost, pleas help me out.

Edit:

I forgot to paste the actual PreferenceFragment:

public class SignUpPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    addPreferencesFromResource(R.xml.preferences);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    return inflater.inflate(R.xml.preferences, container, false);

Thank you!

Answer

singularhum picture singularhum · Apr 6, 2014

You need to remove the onCreateView function in your SignUpPreferenceFragment because you cannot inflate the preferences using the inflater.

That is why you are getting the error stating that there is an error inflating PreferenceScreen because it does not understand that tag/class in the xml.

The inflating of preferences.xml is done by addPreferencesFromResource in onCreate.

Reference