共享首选项错误

时间:2013-03-06 15:36:41

标签: android sharedpreferences

我的活动如下:

public class LogoActivity extends Activity{

SharedPreferences data = getSharedPreferences("la",MODE_PRIVATE);

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_logo);
    SharedPreferences.Editor editor = data.edit();

    for(int i=0;i<5;i++){
        ImageAdapter.items[i] = data.getInt(Integer.toString(i),0);
    }
    name = Names.forDrawable(id);
    check.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {

            a=name.isCorrect(text.getText().toString());
            if(a==true){ 

            ImageAdapter.items[LogoSelectionActivity.pos] = 1;
            Intent intent = new Intent(LogoActivity.this, CorrectActivity.class);
            intent.putExtra ("clicked_position", LogoSelectionActivity.pos);
            startActivity(intent);

            }
            else{
                incorrect.setVisibility(0);
            }

        }
        });

    for(int i = 0;i<5;i++){
        editor.putInt(Integer.toString(i),ImageAdapter.items[i]);
    }
    editor.commit();

    back.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent intent = new Intent(LogoActivity.this, LogoSelectionActivity.class);
            startActivity(intent);  
            }
    });

}
}

我在imageAdapter中持有5个项目的阶段,如0或1.默认值为0.我在我的活动中更改此阶段。更改后,我想保存此阶段,并在用户再次打开应用程序时获取它。我写了代码,但我有错误。我也尝试在代码的最后获得首选项而不是开头,但没有任何改变。

错误讯息:

03-06 17:27:49.520: E/AndroidRuntime(14808): FATAL EXCEPTION: main
03-06 17:27:49.520: E/AndroidRuntime(14808): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{com.example.turkishlogoquiz/com.example.turkishlogoquiz.LogoActivity}: java.lang.NullPointerException
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2024)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2125)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread.access$600(ActivityThread.java:140)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1227)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.os.Handler.dispatchMessage(Handler.java:99)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.os.Looper.loop(Looper.java:137)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread.main(ActivityThread.java:4898)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at java.lang.reflect.Method.invokeNative(Native Method)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at java.lang.reflect.Method.invoke(Method.java:511)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1008)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:775)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at dalvik.system.NativeStart.main(Native Method)
03-06 17:27:49.520: E/AndroidRuntime(14808): Caused by: java.lang.NullPointerException
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:160)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at com.example.turkishlogoquiz.LogoActivity.<init>(LogoActivity.java:32)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at java.lang.Class.newInstanceImpl(Native Method)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at java.lang.Class.newInstance(Class.java:1319)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.Instrumentation.newActivity(Instrumentation.java:1068)
03-06 17:27:49.520: E/AndroidRuntime(14808):    at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2015)

任何帮助?

2 个答案:

答案 0 :(得分:4)

将您的代码更改为:

public class LogoActivity extends Activity{

SharedPreferences data ;  //<<< declare here

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    data = this.getSharedPreferences("la",MODE_PRIVATE); //<<initialize data here
    setContentView(R.layout.activity_logo);
    //...your code here

您需要在onCreate方法

中初始化SharedPreferences实例

答案 1 :(得分:1)

SharedPreferences data = getSharedPreferences("la",MODE_PRIVATE); is wrong

在全球范围内声明共享首选项并将其置于OnCreate()中,如下所示

全球

SharedPreferences data;

并在onCreate

 data= getSharedPreferences("la",MODE_PRIVATE);