1

ブール値の共有設定を使用して、クラスの特定の目標が達成されたかどうかに関するデータを保存するために、共有設定を使用しています。コードのこの部分を実行しようとすると、logcat は次のように出力します。

10-28 00:35:09.771: E/AndroidRuntime(429): FATAL EXCEPTION: main
10-28 00:35:09.771: E/AndroidRuntime(429): java.lang.RuntimeException: Unable to start     activity ComponentInfo{com.selectstartgo.physics281/com.selectstartgo.physics281.Grading}:     java.lang.NullPointerException
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1647)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1663)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread.access$1500(ActivityThread.java:117)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:931)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.os.Handler.dispatchMessage(Handler.java:99)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.os.Looper.loop(Looper.java:123)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread.main(ActivityThread.java:3683)
10-28 00:35:09.771: E/AndroidRuntime(429):  at java.lang.reflect.Method.invokeNative(Native Method)
10-28 00:35:09.771: E/AndroidRuntime(429):  at java.lang.reflect.Method.invoke(Method.java:507)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:839)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:597)
10-28 00:35:09.771: E/AndroidRuntime(429):  at dalvik.system.NativeStart.main(Native Method)
10-28 00:35:09.771: E/AndroidRuntime(429): Caused by: java.lang.NullPointerException
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.preference.PreferenceManager.getDefaultSharedPreferencesName(PreferenceManager.java:353)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.preference.PreferenceManager.getDefaultSharedPreferences(PreferenceManager.java:348)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.selectstartgo.physics281.PhysSharedPrefs.getSharPrefBoolean(PhysSharedPrefs.java:36)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.selectstartgo.physics281.Grading.findCLevel(Grading.java:270)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.selectstartgo.physics281.Grading.initialize(Grading.java:24)
10-28 00:35:09.771: E/AndroidRuntime(429):  at com.selectstartgo.physics281.Grading.onCreate(Grading.java:16)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
10-28 00:35:09.771: E/AndroidRuntime(429):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1611)

これが私の共有設定マネージャーです

package com.selectstartgo.physics281;

import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.preference.PreferenceManager;

public class PhysSharedPrefs {

    public static void putSharPrefInt(Context context, String key, int value) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor edit = pref.edit();
        edit.putInt(key, value);
        edit.commit();
    }

    public static void putSharPrefBoolean(Context context, String key,
            boolean value) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(context);
        Editor edit = pref.edit();
        edit.putBoolean(key, value);
        edit.commit();
    }

    public static int getSharPrefInt(Context context, String key, int _default) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(context);
        return pref.getInt(key, _default);
    }

    public static boolean getSharPrefBoolean(Context context, String key,
            boolean _default) {
        SharedPreferences pref = PreferenceManager
                .getDefaultSharedPreferences(context);
        return pref.getBoolean(key, _default);
    }
}

そして、ここに動作していないコードの塊があります

if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
                "bKey101", false))
            i++;

        if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
                "bKey102", false))
            i++;

        if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
                "bKey103", false))
            i++;

        if (PhysSharedPrefs.getSharPrefBoolean(MyApplication.getAppContext(),
                "bKey104", false)) 

私の MyApplication コードは次のようになります。

package com.selectstartgo.physics281;

import android.app.Application;
import android.content.Context;

public class MyApplication extends Application {

    private static Context context;

    public void onCreate() {
        super.onCreate();
        MyApplication.context = getApplicationContext();
    }

    public static Context getAppContext() {
        return MyApplication.context;
    }

}

私が得ることができる助けをありがとう!

4

2 に答える 2

0

アプリケーション コンテキストへのグローバル アクセス用に のサブクラスを作成したのでApplication、AndroidManifest.xml を編集することを覚えていますか?

<application
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name" 
    android:name="MyApplication">   <!-- This line -->
于 2013-10-28T02:50:48.930 に答える