简体   繁体   中英

Unable to inflate custom preference in preferenceFragment

I continue to get an error when trying to inflate a custom preference. I have tried multiple things but can't get it to work.

public class SettingsFragment extends PreferenceFragment {

    final static String TAG = "Preferance Fragment";

    @Override
    public void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.preferences);
    }
}

preference.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<CheckBoxPreference
    android:key="notifications_on"
    android:title="Notifications"
    android:summary="Display light/group control in notification bar."
    android:defaultValue="true" />

<ListPreference
    android:key="light_transition_speed"
    android:title="Light Transition"
    android:summary="Change the speed at which light changes to new settings"
    android:dialogTitle="Light Transition Speed"
    android:entries="@array/light_transition_speed"
    android:entryValues="@array/light_transition_speed"
    android:defaultValue="Medium" />

<com.mackeytech.test.notifyPreferences
    android:key="tester"/>

</PreferenceScreen>

Here is the notifiyPreferences file:

public class notifyPreferences extends Preference {

//Tag used for logcat
String TAG = "notifyPreferances";

    //Constructor
    notifyPreferences(Context context, AttributeSet attrs) {
        super(context, attrs);
        setLayoutResource(R.layout.preference_line_view);
    }

}

Here is the layout file preference_line_view.xml

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

    <TextView
        android:id="@android:id/title"
     style="@android:style/TextAppearance.DeviceDefault.SearchResult.Title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Title" />

    <TextView
        android:id="@android:id/summary"
        style="@android:style/TextAppearance.DeviceDefault.SearchResult.Subtitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Summary" />
</LinearLayout>

I am assigning it here in the constructor for the custom preference, but I have tried doing it in the xml file by assigning a layout. I have also just used an empty file and tried doing the title, key and summary in the xml file but I can't get the preferenceFragment to load while I have the package com.mackeytech.test.notifyPreferences in the preference file.

Here is the error I am seeing:

11-21 13:49:00.925 5321-5321/? E/AndroidRuntime: FATAL EXCEPTION: main
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime: Process: com.mackeytech.Simplicity, PID: 5321
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.mackeytech.Simplicity/com.mackeytech.test.SettingsActivity}: android.view.InflateException: Binary XML file line #19: Error inflating class com.mackeytech.test.notifyPreferences
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.ActivityThread.access$800(ActivityThread.java:144)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1278)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.os.Handler.dispatchMessage(Handler.java:102)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.os.Looper.loop(Looper.java:135)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.ActivityThread.main(ActivityThread.java:5221)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Native Method)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at java.lang.reflect.Method.invoke(Method.java:372)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:899)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:  Caused by: android.view.InflateException: Binary XML file line #19: Error inflating class com.mackeytech.test.notifyPreferences
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.GenericInflater.createItem(GenericInflater.java:386)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:430)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.GenericInflater.inflate(GenericInflater.java:326)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.GenericInflater.inflate(GenericInflater.java:263)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:299)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at com.mackeytech.test.SettingsFragment.onCreate(SettingsFragment.java:33)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.Fragment.performCreate(Fragment.java:2031)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:863)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1045)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.addFragment(FragmentManager.java:1147)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.FragmentManagerImpl.onCreateView(FragmentManager.java:2116)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.Activity.onCreateView(Activity.java:5286)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.view.LayoutInflater.createViewFromTag(LayoutInflater.java:733)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.view.LayoutInflater.rInflate(LayoutInflater.java:806)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:504)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:414)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.view.LayoutInflater.inflate(LayoutInflater.java:365)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivityDelegateBase.setContentView(ActionBarActivityDelegateBase.java:240)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.support.v7.app.ActionBarActivity.setContentView(ActionBarActivity.java:102)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at com.mackeytech.test.SettingsActivity.onCreate(SettingsActivity.java:14)
11-21 13:49:00.925 5321-5321/? E/AndroidRuntime:     at android.app.Activity.performCreate(Activity.java:5937)

You may be using the wrong type of Preference . Check to see if your PreferenceFragment is a android.preference.PreferenceFragment or android.support.v14.preference.PreferenceFragment . Then match the type of your Preference to the type of your PreferenceFragment .

Right before giving up I found this post about issues with inflation as well.

I just needed to insure my constructor was public and it all appears to work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM