我正在尝试使用preferenceFragments和preferenceActivities创建设置菜单。当我选择“设置”按钮时,我收到应用程序崩溃的错误。我想我可能会遗漏一些东西。有没有明显的错误?谢谢!
我在这里的主要活动中称呼它:
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
Intent i = new Intent(this, UserSettings.class);
startActivity(i);
return true;
}
return super.onOptionsItemSelected(item);
}
这是UserSettings类:
import android.os.Bundle;
import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment;
public class UserSettings extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
getFragmentManager().beginTransaction().replace(android.R.id.content,
new PrefsFragment()).commit();
}
public static class PrefsFragment extends PreferenceFragment{
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.user_settings);
}
}
}
这是user_settings.xml:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" >
<PreferenceCategory android:title="Service Settings">
<CheckBoxPreference
android:defaultValue="true"
android:key="prefService"
android:summary="Enable Service"
android:title="Service" >
</CheckBoxPreference>
</PreferenceCategory>
<PreferenceCategory android:title="Other Settings" >
<CheckBoxPreference
android:defaultValue="false"
android:key="prefLockScreen"
android:summary="Enable Notifications"
android:title="Notify" >
</CheckBoxPreference>
<ListPreference
android:key="prefUpdateFrequency"
android:title="GPS settings"
android:summary="Set GPS Power Usage"
android:entries="@array/updateGPS"
android:entryValues="@array/updateGPSValues"
/>
</PreferenceCategory>
</PreferenceScreen>
我还有一个数组,用于资源中来自updateGPSValues的值。