我正在开发一个需要设置菜单的项目。
我的代码显示没有错误,但是当我尝试按下应用中的Filter片段时,它崩溃了。我对Android比较陌生,所以我不知道我可能做错了什么...这是我的代码:
protected boolean isValidFragment(String fragmentName) {
return PreferenceFragment.class.getName().equals(fragmentName)
|| GeneralPreferenceFragment.class.getName().equals(fragmentName)
|| DataSyncPreferenceFragment.class.getName().equals(fragmentName)
|| NotificationPreferenceFragment.class.getName().equals(fragmentName)
|| ProfilePreferenceFragment.class.getName().equals(fragmentName)
|| FilterPreferenceFragment.class.getName().equals(fragmentName)
|| PrivacyPreferenceFragment.class.getName().equals(fragmentName);
}
这是Filter片段的类:
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public static class FilterPreferenceFragment extends PreferenceFragment {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.pref_filter);
setHasOptionsMenu(true);
// Bind the summaries of EditText/List/Dialog/Ringtone preferences
// to their values. When their values change, their summaries are
// updated to reflect the new value, per the Android Design
// guidelines.
bindPreferenceSummaryToValue(findPreference("example_switch_rest"));
bindPreferenceSummaryToValue(findPreference("example_switch_vend"));
bindPreferenceSummaryToValue(findPreference("example_switch_show"));
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == android.R.id.home) {
startActivity(new Intent(getActivity(), SettingsActivity.class));
return true;
}
return super.onOptionsItemSelected(item);
}
}
这是XML:
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<SwitchPreference
android:defaultValue="true"
android:key="example_switch_rest"
android:summary="@string/pref_description_restaurant"
android:title="@string/pref_title_restaurants" />
<SwitchPreference
android:defaultValue="true"
android:key="example_switch_vend"
android:summary="@string/pref_description_vending"
android:title="@string/pref_title_vending" />
<SwitchPreference
android:defaultValue="true"
android:key="example_switch_show"
android:summary="@string/pref_description_shows"
android:title="@string/pref_title_shows" />
</PreferenceScreen>
这是错误日志:
01-04 13:37:55.864 2828-2828/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
01-04 13:37:55.866 2828-2828/? D/AndroidRuntime: CheckJNI is ON
01-04 13:37:55.879 2828-2828/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
01-04 13:37:55.895 2828-2828/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-04 13:37:55.895 2828-2828/? E/android.os.Debug: failed to load memtrack module: -2
01-04 13:37:55.896 2828-2828/? I/Radio-JNI: register_android_hardware_Radio DONE
01-04 13:37:55.908 2828-2828/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
01-04 13:37:55.913 1300-1453/? I/ActivityManager: Force stopping com.gijbelssiemen.list appid=10056 user=0: from pid 2828
01-04 13:37:55.921 2828-2828/? D/AndroidRuntime: Shutting down VM
01-04 13:37:55.931 2828-2835/? E/art: Thread attaching while runtime is shutting down: Binder_1
01-04 13:37:55.931 2828-2835/? I/AndroidRuntime: NOTE: attach of thread 'Binder_1' failed
01-04 13:37:56.460 2840-2840/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
01-04 13:37:56.461 2840-2840/? D/AndroidRuntime: CheckJNI is ON
01-04 13:37:56.486 2840-2840/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
01-04 13:37:56.492 2836-2836/? D/AndroidRuntime: >>>>>> START com.android.internal.os.RuntimeInit uid 0 <<<<<<
01-04 13:37:56.493 2836-2836/? D/AndroidRuntime: CheckJNI is ON
01-04 13:37:56.512 2836-2836/? D/ICU: No timezone override file found: /data/misc/zoneinfo/current/icu/icu_tzdata.dat
01-04 13:37:56.525 2836-2836/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-04 13:37:56.526 2836-2836/? E/android.os.Debug: failed to load memtrack module: -2
01-04 13:37:56.527 2836-2836/? I/Radio-JNI: register_android_hardware_Radio DONE
01-04 13:37:56.535 2840-2840/? E/memtrack: Couldn't load memtrack module (No such file or directory)
01-04 13:37:56.535 2840-2840/? E/android.os.Debug: failed to load memtrack module: -2
01-04 13:37:56.536 2840-2840/? I/Radio-JNI: register_android_hardware_Radio DONE
01-04 13:37:56.546 2836-2836/? D/AndroidRuntime: Calling main entry com.android.commands.wm.Wm
01-04 13:37:56.548 2836-2836/? D/AndroidRuntime: Shutting down VM
01-04 13:37:56.555 2836-2852/? E/art: Thread attaching while runtime is shutting down: Binder_2
01-04 13:37:56.555 2836-2852/? I/AndroidRuntime: NOTE: attach of thread 'Binder_2' failed
01-04 13:37:56.567 2840-2840/? D/AndroidRuntime: Calling main entry com.android.commands.am.Am
01-04 13:37:56.576 1300-1389/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10000000 cmp=com.gijbelssiemen.list/.SettingsActivity} from uid 0 on display 0
01-04 13:37:56.610 2840-2840/? D/AndroidRuntime: Shutting down VM
01-04 13:37:56.618 1300-1882/? I/ActivityManager: Start proc 2855:com.gijbelssiemen.list/u0a56 for activity com.gijbelssiemen.list/.SettingsActivity
01-04 13:37:56.628 2855-2855/? I/art: Not late-enabling -Xcheck:jni (already on)
01-04 13:37:56.653 2855-2855/? W/System: ClassLoader referenced unknown path: /data/app/com.gijbelssiemen.list-2/lib/x86
01-04 13:37:56.693 952-1355/? E/SurfaceFlinger: ro.sf.lcd_density must be defined as a build property
01-04 13:37:56.695 2855-2868/? D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
01-04 13:37:56.813 2855-2868/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 13:37:56.850 2855-2868/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 13:37:56.850 2855-2868/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xabfaeb20, error=EGL_SUCCESS
01-04 13:37:57.044 2046-2063/? E/Surface: getSlotFromBufferLocked: unknown buffer: 0xab813fb0
01-04 13:37:57.345 1300-1319/? I/ActivityManager: Displayed com.gijbelssiemen.list/.SettingsActivity: +738ms
01-04 13:38:04.887 1300-1312/? I/ActivityManager: START u0 {act=android.intent.action.MAIN cmp=com.gijbelssiemen.list/.SettingsActivity (has extras)} from uid 10056 on display 0
01-04 13:38:04.944 2855-2855/? D/AndroidRuntime: Shutting down VM
01-04 13:38:04.944 2855-2855/? E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.gijbelssiemen.list, PID: 2855
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.gijbelssiemen.list/com.gijbelssiemen.list.SettingsActivity}: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.ClassCastException: java.lang.Boolean cannot be cast to java.lang.String
at android.app.SharedPreferencesImpl.getString(SharedPreferencesImpl.java:225)
at com.gijbelssiemen.list.SettingsActivity.bindPreferenceSummaryToValue(SettingsActivity.java:116)
at com.gijbelssiemen.list.SettingsActivity.access$000(SettingsActivity.java:36)
at com.gijbelssiemen.list.SettingsActivity$FilterPreferenceFragment.onCreate(SettingsActivity.java:297)
at android.app.Fragment.performCreate(Fragment.java:2198)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:942)
at android.app.FragmentManagerImpl.moveToState(FragmentManager.java:1148)
at android.app.BackStackRecord.run(BackStackRecord.java:793)
at android.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1535)
at android.app.FragmentController.execPendingActions(FragmentController.java:325)
at android.app.Activity.performStart(Activity.java:6252)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
01-04 13:38:04.945 1300-1311/? W/ActivityManager: Force finishing activity com.gijbelssiemen.list/.SettingsActivity
01-04 13:38:04.950 1300-1311/? W/ActivityManager: Force finishing activity com.gijbelssiemen.list/.SettingsActivity
01-04 13:38:04.995 1300-1349/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 13:38:05.015 1300-1349/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 13:38:05.015 1300-1349/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0x9c611180, error=EGL_SUCCESS
01-04 13:38:05.459 1300-1314/? W/ActivityManager: Activity pause timeout for ActivityRecord{18dec05 u0 com.gijbelssiemen.list/.SettingsActivity t40 f}
01-04 13:38:05.498 2046-2063/? I/OpenGLRenderer: Initialized EGL, version 1.4
01-04 13:38:05.518 2046-2063/? W/EGL_emulation: eglSurfaceAttrib not implemented
01-04 13:38:05.518 2046-2063/? W/OpenGLRenderer: Failed to set EGL_SWAP_BEHAVIOR on surface 0xa417f0c0, error=EGL_SUCCESS
如果我忘记包含任何内容,请告诉我!谢谢!