SharedPreferences检索错误

时间:2014-06-24 18:56:29

标签: android sharedpreferences

在研究了这个相当彻底之后......我已经得到了我的代码。我见过的所有资源都显示我的代码语法是正确的但是我的应用程序已经强制退出问题..这是我当前的代码:

package com.MonatecSolutions.SafeN_Secure;

import android.app.Activity;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

/**
 * Created by cchance on 6/24/2014.
 */
public class settingActivity extends Activity {
    TextView txt_nameval;
    EditText txt_delayval;
    Button btn_save;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout_settings);

        // Grab the preferences
        final SharedPreferences preferences = getSharedPreferences("myPrefs", 0); // Made final for the onClickListener

        txt_nameval = (TextView) findViewById(R.id.txt_myName);
        txt_delayval = (EditText) findViewById(R.id.txt_delay);
        btn_save = (Button) findViewById(R.id.btnSave);

        // Setting the text from the preferences
        String myName = preferences.getString("myName", "");
        int myDelay = preferences.getInt("delay", 60);
        txt_nameval.setText(myName);
        txt_delayval.setText(myDelay);

        btn_save.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                // Saving the settings and returning to the main screen
                preferences.edit().putString("myName", txt_nameval.getText().toString()).commit();
                preferences.edit().putInt("delay", Integer.parseInt(txt_delayval.getText().toString())).commit();
                startActivity(new Intent(settingActivity.this, MyActivity.class));
            }
        });
    }
}

从logcat获取此信息:

06-24 15:12:50.679    1852-1852/com.MonatecSolutions.SafeN_Secure E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.MonatecSolutions.SafeN_Secure, PID: 1852
java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.MonatecSolutions.SafeN_Secure/com.MonatecSolutions.SafeN_Secure.settingActivity}: java.lang.NullPointerException
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
        at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
        at android.app.ActivityThread.access$800(ActivityThread.java:135)
        at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
        at android.os.Handler.dispatchMessage(Handler.java:102)
        at android.os.Looper.loop(Looper.java:136)
        at android.app.ActivityThread.main(ActivityThread.java:5017)
        at java.lang.reflect.Method.invokeNative(Native Method)
        at java.lang.reflect.Method.invoke(Method.java:515)
        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
        at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
        at dalvik.system.NativeStart.main(Native Method)
 Caused by: java.lang.NullPointerException
        at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173)
        at com.MonatecSolutions.SafeN_Secure.settingActivity.<init>(settingActivity.java:21)
        at java.lang.Class.newInstanceImpl(Native Method)
        at java.lang.Class.newInstance(Class.java:1208)
        at android.app.Instrumentation.newActivity(Instrumentation.java:1061)
        at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
            at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
            at android.app.ActivityThread.access$800(ActivityThread.java:135)
            at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
            at android.os.Handler.dispatchMessage(Handler.java:102)
            at android.os.Looper.loop(Looper.java:136)
            at android.app.ActivityThread.main(ActivityThread.java:5017)
            at java.lang.reflect.Method.invokeNative(Native Method)
            at java.lang.reflect.Method.invoke(Method.java:515)
            at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
            at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
            at dalvik.system.NativeStart.main(Native Method)

我希望有人可以为我阐明这一点。我来自C#背景,所以我可能正在做一些我知道我可以在C#中做的事情,但没有意识到它不能用Java完成。

3 个答案:

答案 0 :(得分:0)

由于您处于Activity中,因此您不需要定义Preference Manager,因为它从Context获取首选项。在创建SharedPreferences变量时,我从未使用过'final'。我还会使用字符串和数字作为参数,如下所示: 编辑:在调用函数之前定义首选项

public class settingActivity extends Activity {
TextView txt_nameval;
EditText txt_delayval;
Button btn_save;
public static final String PREFS_NAME = "MyPrefsFile";
protected SharedPreferences preferences;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_settings);
    preferences = getDefaultSharedPreferences(PREFS_NAME, 0);

有一个很好的例子here

答案 1 :(得分:0)

SharedPreferences创建代码移到onCreate

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.layout_settings);

    preferences = getSharedPreferences("myPrefs", 0);

    //Other stuffs here
}

<强> [EDIT1]
在以下语句中将Integer替换为int

Integer myDelay = preferences.getInt("delay", 60);

<强> [EDIT2]
您是否在Activity档案中注册了manifest

<强> [EDIT3]
罪魁祸首就是这句话:

 txt_delayval.setText(myDelay);

将其替换为:

txt_delayval.setText("" + myDelay);

答案 2 :(得分:0)

//保存

void save() {

SharedPreferences shfObject=getSharedPreferences("savefile",Context.MODE_PRIVATE);


     SharedPreferences.Editor  shfEditorObject=shfObject.edit();

     shfEditorObject.putInt("score", highscore);

}

//负载

void load() {

        SharedPreferences shfObject=getSharedPreferences("savefile",Context.MODE_PRIVATE);

        highscore=shfObject.getInt("score", highscore);

}