Java 跳过某项活动有困难
我的Android应用程序有一个登录屏幕,用户登录到他的仪表板。但我不希望用户每次关闭并启动应用程序时都登录(除非他们从仪表板注销)。因此,我创建了一个类来检查用户是否登录 检查loggedin.javaJava 跳过某项活动有困难,java,android,Java,Android,我的Android应用程序有一个登录屏幕,用户登录到他的仪表板。但我不希望用户每次关闭并启动应用程序时都登录(除非他们从仪表板注销)。因此,我创建了一个类来检查用户是否登录 检查loggedin.java public class CheckLoggedIn extends Activity { private boolean isLoggedIn = false; @Override protected void onCreate(Bundle savedInsta
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;
}
}
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();
}
}
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");
}
......
}
}
用户登录时,我将布尔值isLoggedIn
更改为true
,注销时更改为false
。但当我通过成功登录并关闭并再次启动来检查它时,它仍然会转到LoginActivity。为什么?
编辑:现在,我明白了这是因为我在CheckLoggedIn.java的开头将布尔值isLoggedIn
设置为false
。我怎样才能实现我想做的事
编辑2:
Preference.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;
}
}
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();
}
}
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");
}
......
}
}
MainActivity.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;
}
}
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();
}
}
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
02-17 16:30:53.063:E/AndroidRuntime(21450):致命异常:主
02-17 16:30:53.063:E/AndroidRuntime(21450):进程:collector.lbfinance,PID:21450
02-17 16:30:53.063:E/AndroidRuntime(21450):java.lang.RuntimeException:无法实例化活动组件信息{collector.lbfinance/collector.lbfinance.MainActivity}:java.lang.NullPointerException
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2121)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2245)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread.access$800(ActivityThread.java:135)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread$H.handleMessage(ActivityThread.java:1196)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.os.Handler.dispatchMessage(Handler.java:102)上
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.os.Looper.loop(Looper.java:136)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread.main(ActivityThread.java:5017)上
02-17 16:30:53.063:E/AndroidRuntime(21450):位于java.lang.reflect.Method.Invokenactive(本机方法)
02-17 16:30:53.063:E/AndroidRuntime(21450):位于java.lang.reflect.Method.invoke(Method.java:515)
02-17 16:30:53.063:E/AndroidRuntime(21450):在com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:779)
02-17 16:30:53.063:E/AndroidRuntime(21450):在com.android.internal.os.ZygoteInit.main(ZygoteInit.java:595)
02-17 16:30:53.063:E/AndroidRuntime(21450):在dalvik.system.NativeStart.main(本机方法)
02-17 16:30:53.063:E/AndroidRuntime(21450):由以下原因引起:java.lang.NullPointerException
02-17 16:30:53.063:E/AndroidRuntime(21450):位于android.content.ContextWrapper.getSharedReferences(ContextWrapper.java:173)
02-17 16:30:53.063:E/AndroidRuntime(21450):在collector.lbfinance.library.Preference.(Preference.java:13)
02-17 16:30:53.063:E/AndroidRuntime(21450):在collector.lbfinance.MainActivity。(MainActivity.java:52)
02-17 16:30:53.063:E/AndroidRuntime(21450):位于java.lang.Class.newInstanceImpl(本机方法)
02-17 16:30:53.063:E/AndroidRuntime(21450):位于java.lang.Class.newInstance(Class.java:1208)
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.Instrumentation.newActivity(Instrumentation.java:1061)上
02-17 16:30:53.063:E/AndroidRuntime(21450):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2112)
02-17 16:30:53.063:E/AndroidRuntime(21450):。。。还有11个
您可以使用存储凭据的值
在SharedReference中存储值的步骤
SharedPreferences preferences = getSharedPreferences("userData", 0);
SharedPreferences.Editor editor = preferences.edit();
editor.putString("userName", "value");
editor.putString("password", "value");
editor.commit();
要从SharedReference检索值
SharedPreferences preferences = getSharedPreferences("loginData", 0);
String userName = preferences.getString("userName", "");
String password = preferences.getString("password", "");
为此,您必须使用。布尔变量仅在活动未销毁之前有效。若要持久化数据,必须使用共享首选项或
只有当我们有关系数据库时才建议使用sqlite,对于应用程序搜索等共享首选项的轻量级持久存储,最好使用sqlite
编辑
在活动中,调用此函数并按以下方式向其传递上下文:
PreferenceForApp myPrefs= new PreferenceForApp (this);
Boolean val=myPrefs.getIsDeviceValidated();
使用
SharedReferences
存储isLoggedIn
的值(或更适合您需要的任何其他数据存储选项)。这里有更多信息:为什么要创建一个无用的不必要的类。只需使用共享首选项,在布尔变量中将登录名的值保存为true,然后通过…尝试此方法。但是它在prefs=context.getSharedReferences(“myAppPrefs”,0)行给出了一个NullPointerException;我仍然得到NullPointerException。在我的main活动中,我完成了上述操作(我指的是调用函数)。但这个应用程序甚至没有启动。它立即崩溃。调试时,“调试”窗口显示添加到主目录的ContextWrapper中未找到的源question@ThahzanMohomed移动Preference Preference=新的首选项(此)
到onCreate()
-您不能在laalto正确指出的onCreate()
@ThahzanMohomed之前将活动用作上下文。只需在创建时将所述行移入即可。