无法单击PreferenceFragment中的ListPreference对话框

时间:2016-08-10 16:24:44

标签: android nullpointerexception listpreference preferencefragment

我正在尝试在片段中使用ListPreference项 我有以下片段:

public class PreferencesFragment extends PreferenceFragment
{

    public PreferencesFragment() {
    }


    public static PreferencesFragment newInstance() {
        PreferencesFragment fragment = new PreferencesFragment();
        return fragment;
    }

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

}

我的preferences.xml看起来如下:

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

<PreferenceScreen
    xmlns:android="http://schemas.android.com/apk/res/android"
    >
    <ListPreference
        android:title="Test"
        android:key="list_contacts"
        android:defaultValue="0"
        android:entries="@array/list_entries"
        android:entryValues="@array/list_values"/>
</PreferenceScreen>

我的arrays.xml是这样的:

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string-array name="list_entries">
        <item>Item1</item>
        <item>Item2</item>
        <item>Item3</item>
    </string-array>

    <string-array name="list_values">
        <item>0</item>
        <item>1</item>
        <item>2</item>
    </string-array>
</resources>

我将PreferencesFragment附加到我的MainActivity中 它会显示,但是当我点击该项目时,带有项目列表的对话框不会显示。

相反,我收到以下错误输出:

java.lang.NullPointerException: Attempt to invoke virtual method 'android.view.View android.view.View.findViewById(int)' on a null object reference
 at com.android.internal.app.AlertController.setupView(AlertController.java:472)
 at com.android.internal.app.AlertController.installContent(AlertController.java:237)
 at android.app.AlertDialog.onCreate(AlertDialog.java:423)
 at android.app.Dialog.dispatchOnCreate(Dialog.java:389)
 at android.app.Dialog.show(Dialog.java:293)
 at android.preference.DialogPreference.showDialog(DialogPreference.java:319)
 at android.preference.DialogPreference.onClick(DialogPreference.java:277)
 at android.preference.Preference.performClick(Preference.java:994)
 at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:214)
 at android.widget.AdapterView.performItemClick(AdapterView.java:310)
 at android.widget.AbsListView.performItemClick(AbsListView.java:1155)
 at android.widget.AbsListView$PerformClick.run(AbsListView.java:3126)
 at android.widget.AbsListView$3.run(AbsListView.java:4041)
 at android.os.Handler.handleCallback(Handler.java:751)
 at android.os.Handler.dispatchMessage(Handler.java:95)
 at android.os.Looper.loop(Looper.java:154)
 at android.app.ActivityThread.main(ActivityThread.java:6077)
 at java.lang.reflect.Method.invoke(Native Method)
 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:865)
 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:755)

并且应用程序停止。

我在做什么?我该如何解决这个问题?

我已经查看过这里的所有主题,但没有任何效果,我的脑海里再也没有了。

修改 我在Activity中切换的片段非常复杂,但总而言之,它是这样的:

private void navigatePreferences() {
    preferencesFragment = PreferencesFragment.newInstance();
    FragmentTransaction ft = getFragmentManager().beginTransaction();
    ft.replace(R.id.mainFragmentPlace, preferencesFragment);
    ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN);
    ft.addToBackStack(null);
    ft.commit();
}

0 个答案:

没有答案