我的Android应用程序具有登录屏幕,并且用户登录到他的仪表板。但是我不希望用户每次关闭应用程序并启动时都进行登录(除非他们从仪表板上注销)。因此,我创建了一个类来检查用户是否登录
CheckLoggedIn.java
public class CheckLoggedIn extends Activity { private boolean isLoggedIn = false; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); if(!isLoggedIn){ Intent intent = new Intent(this, MainActivity.class); startActivity(intent); }else{ Intent intent = new Intent(this, AgentHome.class); startActivity(intent); } } public boolean getStatus(){ return this.isLoggedIn; } public void setStatus(boolean status){ isLoggedIn = status; } }
当用户登录时,我将布尔值更改为isLoggedIn,true并false在注销时更改。但是,当我通过成功登录并关闭并再次启动进行检查时,它仍然进入LoginActivity。为什么?
isLoggedIn
true
false
编辑:现在,我明白这是因为我设置的布尔isLoggedIn到false在CheckLoggedIn.java的开始。我如何才能实现自己的目标?
编辑2:
Preference.java
public class Preference { Context context; SharedPreferences sharedPref; public Preference(Context context){ this.context = context; sharedPref = context.getSharedPreferences("LoginState", 0); } public boolean getIsLoggedIn(){ return sharedPref.getBoolean("State", false); } public void setIsLoggedIn(boolean state){ SharedPreferences.Editor editor = sharedPref.edit(); editor.putBoolean("State", state); editor.commit(); } }
MainActivity.java
public class MainActivity extends Activity { Preference preference = new Preference(this); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); if(preference.getIsLoggedIn()){ Log.d("State", "Already logged in"); } ...... } }
Logcat
02-17 16:30:53.063: E/AndroidRuntime(21450): FATAL EXCEPTION: main 02-17 16:30:53.063: E/AndroidRuntime(21450): Process: collector.lbfinance, PID: 21450 02-17 16:30:53.063: E/AndroidRuntime(21450): java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{collector.lbfinance/collector.lbfinance.MainActivity}: java.lang.NullPointerException 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.access$800(ActivityThread.java:135) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.os.Handler.dispatchMessage(Handler.java:102) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.os.Looper.loop(Looper.java:136) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.main(ActivityThread.java:5017) 02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.reflect.Method.invokeNative(Native Method) 02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.reflect.Method.invoke(Method.java:515) 02-17 16:30:53.063: E/AndroidRuntime(21450): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779) 02-17 16:30:53.063: E/AndroidRuntime(21450): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595) 02-17 16:30:53.063: E/AndroidRuntime(21450): at dalvik.system.NativeStart.main(Native Method) 02-17 16:30:53.063: E/AndroidRuntime(21450): Caused by: java.lang.NullPointerException 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.content.ContextWrapper.getSharedPreferences(ContextWrapper.java:173) 02-17 16:30:53.063: E/AndroidRuntime(21450): at collector.lbfinance.library.Preference.<init>(Preference.java:13) 02-17 16:30:53.063: E/AndroidRuntime(21450): at collector.lbfinance.MainActivity.<init>(MainActivity.java:52) 02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.Class.newInstanceImpl(Native Method) 02-17 16:30:53.063: E/AndroidRuntime(21450): at java.lang.Class.newInstance(Class.java:1208) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.Instrumentation.newActivity(Instrumentation.java:1061) 02-17 16:30:53.063: E/AndroidRuntime(21450): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112) 02-17 16:30:53.063: E/AndroidRuntime(21450): ... 11 more
为此,您将必须使用共享首选项。布尔变量仅在不破坏活动之前才有效。要保留数据,必须使用共享首选项或sqlite。
仅在拥有关系数据库的情况下才建议使用sqlite,因为轻量级持久存储应用程序种子等共享首选项是最好的。
public class PreferenceForApp { Context context; SharedPreferences prefs; public PreferenceForApp(Context context) { this.context = context; prefs = context.getSharedPreferences("myAppPrefs", 0); } public Boolean getIsDeviceValidated() { return prefs.getBoolean("Validated", false); } public void setIsDeviceValidated(Boolean value) { SharedPreferences.Editor editor = prefs.edit(); editor.putBoolean("Validated", value); editor.commit(); } }
编辑
在您的Activity中调用此函数,并通过以下方式将上下文传递给它:
PreferenceForApp myPrefs= new PreferenceForApp (this); Boolean val=myPrefs.getIsDeviceValidated();