1

我有一个 CheckBoxPreference(key=time_remaining_vibrate) 首选项和一个 MultiSelectListPreference(key=time_remaining_vibrate_times),我希望 MultiSelectListPreference 依赖于 CheckBoxPreference 但是每当我将“android:dependency="time_remaining_vibrate"”添加到 XML 文件时,我应用程序崩溃。奇怪的是,如果我将 MultiSelectListPreference 更改为 ListPreference,那么它不再崩溃,但我需要 MultiSelect 首选项。以下是相关文件,可能还有一些不相关的文件:)

编辑:我的 API 版本是 19,如果这很重要,所以我知道 MultiSelectListPreference 是受支持的。

编辑:解决方案:我需要将 android:defaultValue="@array/empty_array" 添加到 MultiSelectListPreference。感谢 Michiyo 的回答:)。我遇到的另一个问题是无法将字符串转换为设置异常,但这已通过清除设置中的应用程序数据来解决。

首选项.xml:

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

<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">


<CheckBoxPreference
        android:key="early"
        android:title="@string/early"
        android:summary="@string/early_summ"
        android:defaultValue="false"/>

<PreferenceCategory
        android:title="@string/time_remaining"
        android:key="time_remaining">

    <CheckBoxPreference
            android:key="time_remaining_vibrate"
            android:title="@string/remain_vibrate"
            android:summary="@string/remain_vibrate_summ"
            android:disableDependentsState="true"
            android:defaultValue="false"/>

    <MultiSelectListPreference
            android:key="time_remaining_vibrate_times"
            android:title="@string/remain_vibrate_times"
            android:summary="@string/remain_vibrate_times_summ"
            android:entries="@array/empty_array"
            android:entryValues="@array/empty_array"
            android:dependency="time_remaining_vibrate"/>

</PreferenceCategory>


</PreferenceScreen>

SettingsFragment.java

import android.os.Bundle;
import android.preference.PreferenceFragment;


public class SettingsFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Load the preferences from an XML resource
    addPreferencesFromResource(R.xml.preferences);
}

}

设置活动.java:

import android.app.Activity;
import android.os.Bundle;
import android.preference.PreferenceManager;


public class SettingsActivity extends Activity {

public static final String PREFS_NAME = "userSettings.txt";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new SettingsFragment())
            .commit();

    PreferenceManager.setDefaultValues(this,R.xml.preferences,false);
}


}

日志:

    java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.Schedule/com.example.Schedule.SettingsActivity}: android.view.InflateException: Binary XML file line #23: Error inflating class java.lang.reflect.Constructor
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2585)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.view.InflateException: Binary XML file line #23: Error inflating class java.lang.reflect.Constructor
at android.preference.GenericInflater.createItem(GenericInflater.java:397)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at com.example.Schedule.SettingsFragment.onCreate(SettingsFragment.java:21)
at android.app.Fragment.performCreate(Fragment.java:1755)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:868)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
at android.app.BackStackRecord.run(BackStackRecord.java:690)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
at android.app.Activity.performStart(Activity.java:5321)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.reflect.InvocationTargetException
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at com.example.Schedule.SettingsFragment.onCreate(SettingsFragment.java:21)
at android.app.Fragment.performCreate(Fragment.java:1755)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:868)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
at android.app.BackStackRecord.run(BackStackRecord.java:690)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
at android.app.Activity.performStart(Activity.java:5321)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)
Caused by: java.lang.NullPointerException
at android.preference.MultiSelectListPreference.onGetDefaultValue(MultiSelectListPreference.java:212)
at android.preference.Preference.<init>(Preference.java:268)
at android.preference.DialogPreference.<init>(DialogPreference.java:69)
at android.preference.DialogPreference.<init>(DialogPreference.java:90)
at android.preference.MultiSelectListPreference.<init>(MultiSelectListPreference.java:49)
at java.lang.reflect.Constructor.constructNative(Native Method)
at java.lang.reflect.Constructor.newInstance(Constructor.java:423)
at android.preference.GenericInflater.createItem(GenericInflater.java:383)
at android.preference.GenericInflater.onCreateItem(GenericInflater.java:417)
at android.preference.GenericInflater.createItemFromTag(GenericInflater.java:428)
at android.preference.GenericInflater.rInflate(GenericInflater.java:481)
at android.preference.GenericInflater.rInflate(GenericInflater.java:493)
at android.preference.GenericInflater.inflate(GenericInflater.java:326)
at android.preference.GenericInflater.inflate(GenericInflater.java:263)
at android.preference.PreferenceManager.inflateFromResource(PreferenceManager.java:272)
at android.preference.PreferenceFragment.addPreferencesFromResource(PreferenceFragment.java:285)
at com.example.Schedule.SettingsFragment.onCreate(SettingsFragment.java:21)
at android.app.Fragment.performCreate(Fragment.java:1755)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:868)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1106)
at android.app.BackStackRecord.run(BackStackRecord.java:690)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1571)
at android.app.Activity.performStart(Activity.java:5321)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2558)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2641)
at android.app.ActivityThread.access$800(ActivityThread.java:156)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1355)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5867)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:674)
at dalvik.system.NativeStart.main(Native Method)

如果有人能指出我可能是简单错误的方向,或者我的应用程序崩溃的原因,我将永远感激不尽!

谢谢

4

0 回答 0