0

When I click Settings in my application's menu, app crash instantly. I don't have idea how to repair it.

I am new to Android and I learn from Hello, Android book.

Application files below:


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" />

&lt;/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
4

2 回答 2

2

The problem is that you need to extend PreferenceActivity, not PreferenceFragment. PreferenceActivity extends ListActivity, which extends Activity, while PreferenceFragment extends Fragment.

That's why you have the ClassCast Exception.

于 2012-06-26T14:30:32.500 回答
0

How did you access to your PreferenceFragment ?

Is it contained in a FragmentActivity ?

You cannot call directly a Fragment with startActivity();

于 2012-06-26T14:31:17.120 回答