1

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

    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();
    }  
}

但它的力量越来越近了。:(

这是我的日志猫

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)
4

3 回答 3

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();
    }  
}
于 2012-07-25T08:55:26.053 回答
0

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

于 2012-07-25T08:38:15.960 回答
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
    }
于 2012-07-25T09:03:45.417 回答