导航时PreferenceFragment / Activity崩溃

时间:2015-10-31 16:11:50

标签: android sharedpreferences android-preferences preferenceactivity

我已经实现了一个简单的PreferencesFragment。但是当用户导航回来时,应用程序崩溃了。

PreferencesFragment类:

public class AppPreferencesActivityFragment extends PreferenceFragment {

public AppPreferencesActivityFragment() {
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

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

}

}

托管活动:

public class AppPreferencesActivity extends ABBActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_app_preferences);

    // Display the fragment as the main content.
    getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new AppPreferencesActivityFragment())
            .commit();
}

@Override
public void onBackPressed() {
    super.onBackPressed();
}

}

这是preferences.xml

<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<EditTextPreference
    android:key="firstname"
    android:title="@string/firstname_"
    android:defaultValue=""></EditTextPreference>
<EditTextPreference
    android:key="email"
    android:title="@string/lastname_"
    android:defaultValue=""></EditTextPreference>
<EditTextPreference
    android:key="email"
    android:title="@string/email_adresse_"
    android:defaultValue=""></EditTextPreference>

logcat的:

  

10-31 16:32:50.374 24512-24512 /? E / AndroidRuntime:致命异常:主要       显示java.lang.NullPointerException               在android.preference.PreferenceFragment $ 3.onKey(PreferenceFragment.java:368)               在android.view.View.dispatchKeyEvent(View.java:7200)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1354)               在android.widget.ListView.dispatchKeyEvent(ListView.java:2088)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在android.view.ViewGroup.dispatchKeyEvent(ViewGroup.java:1359)               在com.android.internal.policy.impl.PhoneWindow $ DecorView.superDispatchKeyEvent(PhoneWindow.java:1920)               在com.android.internal.policy.impl.PhoneWindow.superDispatchKeyEvent(PhoneWindow.java:1395)               在android.app.Activity.dispatchKeyEvent(Activity.java:2370)               在android.support.v7.internal.view.WindowCallbackWrapper.dispatchKeyEvent(WindowCallbackWrapper.java:50)               在android.support.v7.app.AppCompatDelegateImplBase $ AppCompatWindowCallbackBase.dispatchKeyEvent(AppCompatDelegateImplBase.java:250)               在com.android.internal.policy.impl.PhoneWindow $ DecorView.dispatchKeyEvent(PhoneWindow.java:1847)               在android.view.ViewRootImpl.deliverKeyEventPostIme(ViewRootImpl.java:3701)               在android.view.ViewRootImpl.handleImeFinishedEvent(ViewRootImpl.java:3651)               在android.view.ViewRootImpl $ ViewRootHandler.handleMessage(ViewRootImpl.java:2818)               在android.os.Handler.dispatchMessage(Handler.java:99)               在android.os.Looper.loop(Looper.java:137)               在android.app.ActivityThread.main(ActivityThread.java:5041)               at java.lang.reflect.Method.invokeNative(Native Method)               在java.lang.reflect.Method.invoke(Method.java:511)               在com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:793)               在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)               在dalvik.system.NativeStart.main(本地方法)

有人可以帮忙吗?

2 个答案:

答案 0 :(得分:0)

将片段事务添加到backstack,如下所示:

getFragmentManager().beginTransaction()
            .replace(android.R.id.content, new AppPreferencesActivityFragment())
            .addToBackStack(null)//Add this transaction to the back stack.
            .commit();

更改为:

@Override
public void onBackPressed() {
    if (getFragmentManager().getBackStackEntryCount() != 0) {
        getFragmentManager().popBackStack();
    }
}

答案 1 :(得分:0)

问题是Amdroid Studio自动生成的一行代码,我没有注意到:

在活动中,删除:

setContentView(R.layout.activity_app_preferences);

修好了。