使用共享首选项保存切换状态

时间:2012-07-25 08:30:40

标签: android toggle sharedpreferences

我尝试在我的应用程序中保存切换状态。这是我的代码

    boolean on;
public SharedPreferences spref;
ToggleButton tb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fb_intermidiate);
    spref = getPreferences(MODE_PRIVATE);
    tb = (ToggleButton) findViewById(R.id.toggleButton1);
    on = spref.getBoolean("On", true);  //default is true
    if (on = true) 
    {
      tb.setChecked(true);
    }       else
    {
      tb.setChecked(false);
    }
    back = (Button)findViewById(R.id.button_back); 
    //back.setText(R.string.back_button_in_settings);
    back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    }); 
} 

public void onToggleClicked(View view) {

    on = ((ToggleButton) view).isChecked();
    if (on) {
        Toast.makeText(this, "On : Notification will be Enabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("On", true); // value to store
        editor.commit();

    } else {
    Toast.makeText(this, "Off : Notification will be Disabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor =spref.edit();
        editor.putBoolean("Off", false); // value to store
        editor.commit();
    }  
}

但它的力量接近了。:(

这是我的logcat

07-25 09:02:23.317: E/AndroidRuntime(640): java.lang.RuntimeException: Unable to start       activity ComponentInfo{com.ace.gugulog/com.ace.gugulog.activities.FacebookIntermidiate}: java.lang.NullPointerException

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1815)

07-25 09:02:23.317: E/AndroidRuntime(640):  at java.lang.reflect.Method.invokeNative(Native Method)

07-25 09:02:23.317: E/AndroidRuntime(640):  at java.lang.reflect.Method.invoke(Method.java:491)

07-25 09:02:23.317: E/AndroidRuntime(640):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:599)

07-25 09:02:23.317: E/AndroidRuntime(640):  at dalvik.system.NativeStart.main(Native Method)
07-25 09:02:23.317: E/AndroidRuntime(640): Caused by: java.lang.NullPointerException

07-25 09:02:23.317: E/AndroidRuntime(640):  at  com.ace.gugulog.activities.FacebookIntermidiate.onCreate(FacebookIntermidiate.java:32)

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.Activity.performCreate(Activity.java:4397)

07-25 09:02:23.317: E/AndroidRuntime(640):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1048)

3 个答案:

答案 0 :(得分:1)

我认为最有可能的错误来源是,您没有正确使用共享首选项,将代码更改为:您应该使用getSharedPreferences而不是getPreferences方法。

  boolean on;
public SharedPreferences spref;
final String PREF_NAME="preferences";
ToggleButton tb;

@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.fb_intermidiate);
    spref = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
    tb = (ToggleButton) findViewById(R.id.toggleButton1);
    on = spref.getBoolean("On", true);  //default is true
    if (on = true) 
    {
      tb.setChecked(true);
    }       else
    {
      tb.setChecked(false);
    }
    back = (Button)findViewById(R.id.button_back); 
    //back.setText(R.string.back_button_in_settings);
    back.setOnClickListener(new View.OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            finish();
        }
    }); 
} 

public void onToggleClicked(View view) {

    on = ((ToggleButton) view).isChecked();
    if (on) {
        Toast.makeText(this, "On : Notification will be Enabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor = spref.edit();
        editor.putBoolean("On", true); // value to store
        editor.commit();

    } else {
    Toast.makeText(this, "Off : Notification will be Disabled", Toast.LENGTH_SHORT).show();
        SharedPreferences.Editor editor =spref.edit();
        editor.putBoolean("Off", false); // value to store
        editor.commit();
    }  
}

答案 1 :(得分:0)

如果(on)或if(on == true),你应该检查if条件。我不是导致问题的原因,但它是其中之一

答案 2 :(得分:0)

你可以试试这种方式

SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
SharedPreferences.Editor editor = sharedPreferences.edit();

Boolean checkforFirstTime = sharedPreferences.getBoolean("checkforFirstTime", true);

    //Checking whether application is launched first time
    if (checkforFirstTime) {
        // Make boolean value "checkforFirstTime" to false
        editor.putBoolean("checkforFirstTime", false);
        editor.commit();
    }
    } else {
              // else part
    }