无法在preferenceFragment中夸大自定义首选项

时间:2015-11-22 23:06:04

标签: android preferenceactivity

尝试充气自定义偏好时,我仍然遇到错误。我尝试了很多东西,却无法让它发挥作用。

<?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>

preference.xml

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

}

这是notifiyPreferences文件:

<?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>

这是布局文件preference_line_view.xml

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)

我在自定义首选项的构造函数中分配它,但我已尝试通过分配布局在xml文件中执行此操作。我还使用了一个空文件并尝试在xml文件中执行标题,键和摘要但是我在首选项文件中包含com.mackeytech.test.notifyPreferences包时无法加载preferenceFragment。

以下是我看到的错误:

Type

1 个答案:

答案 0 :(得分:1)

您可能使用了错误类型的Preference。检查您的PreferenceFragmentandroid.preference.PreferenceFragment还是android.support.v14.preference.PreferenceFragment。然后将Preference的类型与PreferenceFragment的类型进行匹配。