10

这是我PreferenceActivity的(我的主要活动的内部课程)

public static class TestSettings extends PreferenceActivity implements Preference.OnPreferenceClickListener{

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        addPreferencesFromResource(R.xml.pref);
        getPreferenceManager().findPreference("key").setOnPreferenceClickListener(this);

    }

这是我的onPreferenceClick

@Override
public boolean onPreferenceClick(Preference preference) {
    FragmentClass fc = new FragmentClass();
    fc.show(fm, "TAG");
    return false;
}

这是我的FragmentClassTimePicker对话框):

public class FragmentClass extends DialogFragment implements TimePickerDialog.OnTimeSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new TimePickerDialog(getActivity(), this, 15, 00, false);
    }


    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
        Log.i("APP", "hour: "+hourOfDay+", min: "+minute);
    }
}

问题:

当我单击首选项(在 XML 中定义),当我处理onClick(我正在显示TimePickerDialog onClick)时,我得到以下异常:

  03-21 10:06:41.325: E/AndroidRuntime(11003): FATAL EXCEPTION: main
03-21 10:06:41.325: E/AndroidRuntime(11003): java.lang.IllegalStateException: Can not perform this action after onSaveInstanceState
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.checkStateLoss(FragmentManager.java:1327)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.FragmentManagerImpl.enqueueAction(FragmentManager.java:1338)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commitInternal(BackStackRecord.java:595)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.BackStackRecord.commit(BackStackRecord.java:574)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.support.v4.app.DialogFragment.show(DialogFragment.java:127)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.example.timepicker2.MainActivity$TestSettings.onPreferenceClick(MainActivity.java:110)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.Preference.performClick(Preference.java:951)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.preference.PreferenceScreen.onItemClick(PreferenceScreen.java:215)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AdapterView.performItemClick(AdapterView.java:298)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.widget.AbsListView$1.run(AbsListView.java:3423)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.handleCallback(Handler.java:725)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Handler.dispatchMessage(Handler.java:92)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.os.Looper.loop(Looper.java:137)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at android.app.ActivityThread.main(ActivityThread.java:5041)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invokeNative(Native Method)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at java.lang.reflect.Method.invoke(Method.java:511)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
03-21 10:06:41.325: E/AndroidRuntime(11003):    at dalvik.system.NativeStart.main(Native Method)

代码错误指向方法show()中的onPreferenceClick方法。

为什么会发生这种情况,我该如何解决?

编辑:

Prefs.xml 文件

    <?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >


    <CheckBoxPreference android:title="Checkbox" android:summary="Checkbox"/>
    <EditTextPreference android:summary="time" android:dialogTitle="Hello"  />
    <Preference android:title="Prefff" android:key="key"/>
</PreferenceScreen>

我试过的:

我尝试了大多数投票的解决方案,我发现这里得到异常“IllegalStateException:onSaveInstanceState 后无法执行此操作”

   @Override
protected void onSaveInstanceState(Bundle outState) {
    //No call for super(). Bug on API Level > 11.
}

但它仍然无法正常工作。甚至在 API < 11 上也没有。我仍然提到了异常。

4

1 回答 1

1

在这里有点勉强:

// this is in your TestSettings class
public boolean onPreferenceClick(Preference preference) {
    FragmentClass fc = new FragmentClass();
    // getFragmentManager() here ensures you are using the FragmentManager
    // associated with TestSettings activity and not your MainActivity
    fc.show(getFragmentManager(), "TAG");
    return false;
}

关键是getFragmentManager()

确保您使用的是FragmentManager与 TestSettings 关联的活动。我有一种感觉,您正在使用FragmentManagerMainActivity 中的实例(因为 TestSettings 是 MainActivity 的内部类,这并不是一个疯狂的猜测......也许)。

于 2013-04-03T07:09:19.283 回答