我尝试在我的应用程序中保存切换状态。这是我的代码
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)