打开首选项后应用程序崩溃

时间:2012-06-26 14:17:25

标签: android android-intent android-preferences

当我在应用程序菜单中单击“设置”时,应用程序会立即崩溃。我不知道如何修复它。

我是Android新手,我从 Hello,Android 一书中学习。

以下申请文件:


PrefsFragment.java:

package org.example.sudoku;

import android.os.Bundle;
import android.preference.PreferenceFragment;

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

RES / XML /的preferences.xml:

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

    <CheckBoxPreference
        android:key="music"
        android:title="@string/music_title"
        android:summary="@string/music_summary"
        android:defaultValue="true" />

    <CheckBoxPreference
        android:key="hints"
        android:title="@string/hints_title"
        android:summary="@string/hints_summary"
        android:defaultValue="true" />

</PreferenceScreen>

logcat的:

06-26 16:14:26.600: W/dalvikvm(2827): threadid=1: thread exiting with uncaught exception (group=0x40a041f8)
06-26 16:14:26.610: E/AndroidRuntime(2827): FATAL EXCEPTION: main
06-26 16:14:26.610: E/AndroidRuntime(2827): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{org.example.sudoku/org.example.sudoku.PrefsFragment}: java.lang.ClassCastException: org.example.sudoku.PrefsFragment cannot be cast to android.app.Activity
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1993)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread.access$600(ActivityThread.java:132)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1157)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.os.Handler.dispatchMessage(Handler.java:99)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.os.Looper.loop(Looper.java:137)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread.main(ActivityThread.java:4575)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at java.lang.reflect.Method.invokeNative(Native Method)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at java.lang.reflect.Method.invoke(Method.java:511)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:789)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:556)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at dalvik.system.NativeStart.main(Native Method)
06-26 16:14:26.610: E/AndroidRuntime(2827): Caused by: java.lang.ClassCastException: org.example.sudoku.PrefsFragment cannot be cast to android.app.Activity
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.Instrumentation.newActivity(Instrumentation.java:1023)
06-26 16:14:26.610: E/AndroidRuntime(2827):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1984)
06-26 16:14:26.610: E/AndroidRuntime(2827):     ... 11 more

2 个答案:

答案 0 :(得分:2)

问题是您需要扩展PreferenceActivity,而不是PreferenceFragment。 PreferenceActivity扩展了ListActivity,它扩展了Activity,而PreferenceFragment扩展了Fragment。

这就是你有ClassCast异常的原因。

答案 1 :(得分:0)

您是如何访问PreferenceFragment的?

它是否包含在FragmentActivity中?

您无法使用startActivity();

直接调用片段