在我的代码中,我有MainActivity
作为启动器活动。通过从checkLogin()
调用SessionManagementActivity
函数,它首先检查我们登录的位置。调用该函数,并满足"未登录"的条件。因此,它应该从LoginActivity
开始,然后MainActivity
结束FLAG_CLEAR_TOP
。但控件再次传递给MainActivity
,并在checkLogin()
调用后开始执行更多代码。也就是说,MainActivity
并未从SessionManagementActivity
关闭。因此,我的应用首先没有响应,它意外关闭,然后也成功启动LoginActivity
。当我回击时,它会在移动到MainActivity
时意外关闭。任何人都可以说,为什么MainActivity
之后CheckLogin()
没有关闭?
这是代码
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
session = new SessionManagement(getApplicationContext());
session.checkLogin();
HashMap<String, String> user = session.getUserDetails();
username = user.get(SessionManagement.KEY_NAME);
school = user.get(SessionManagement.KEY_SCHOOL);
SessionManagement.java
// Editor for Shared preferences
Editor editor;
// Context
Context _context;
// Shared pref mode
int PRIVATE_MODE = 0;
// Sharedpref file name
private static final String PREF_NAME = "iguardian";
// All Shared Preferences Keys
private static final String IS_LOGIN = "IsLoggedIn";
// User name (make variable public to access from outside)
public static final String KEY_NAME = "name";
// Email address (make variable public to access from outside)
public static final String KEY_SCHOOL = "school";
// Constructor
public SessionManagement(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
/**
* Create login session
* */
public void createLoginSession(String username, String school){
// Storing login value as TRUE
editor.putBoolean(IS_LOGIN, true);
// Storing name in pref
editor.putString(KEY_NAME, username);
// Storing email in pref
editor.putString(KEY_SCHOOL, school);
// commit changes
editor.commit();
}
/**
* Check login method wil check user login status
* If false it will redirect user to login page
* Else won't do anything
* */
public void checkLogin(){
Log.i("Value of ","check");
// Check login status
if(!this.isLoggedIn()){
Intent i = new Intent(_context, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
}
}
日志
01-01 19:17:51.617: E/AndroidRuntime(12017): FATAL EXCEPTION: main
01-01 19:17:51.617: E/AndroidRuntime(12017): Process: com.litchi.iguardian, PID: 12017
01-01 19:17:51.617: E/AndroidRuntime(12017): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.litchi.iguardian/com.litchi.iguardian.MainActivity}: java.lang.NullPointerException
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2198)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2257)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread.access$800(ActivityThread.java:139)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1210)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.os.Handler.dispatchMessage(Handler.java:102)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.os.Looper.loop(Looper.java:136)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread.main(ActivityThread.java:5086)
01-01 19:17:51.617: E/AndroidRuntime(12017): at java.lang.reflect.Method.invokeNative(Native Method)
01-01 19:17:51.617: E/AndroidRuntime(12017): at java.lang.reflect.Method.invoke(Method.java:515)
01-01 19:17:51.617: E/AndroidRuntime(12017): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
01-01 19:17:51.617: E/AndroidRuntime(12017): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
01-01 19:17:51.617: E/AndroidRuntime(12017): at dalvik.system.NativeStart.main(Native Method)
01-01 19:17:51.617: E/AndroidRuntime(12017): Caused by: java.lang.NullPointerException
01-01 19:17:51.617: E/AndroidRuntime(12017): at com.litchi.iguardian.MainActivity.onCreate(MainActivity.java:152)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.Activity.performCreate(Activity.java:5248)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1110)
01-01 19:17:51.617: E/AndroidRuntime(12017): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2162)
01-01 19:17:51.617: E/AndroidRuntime(12017): ... 11 more
答案 0 :(得分:0)
从 checkLogin()获取布尔结果,并从 MainActivity 开始活动 从
更改声明_context.startActivity(i);
到
_context.startActivityForResult(i, requestCode);
参考此startActivityForResult
http://developer.android.com/training/basics/intents/result.html
编辑2: 的 SessionManagement.java 强>
public boolean checkLogin(){
Log.i("Value of ","check");
// Check login status
if(!this.isLoggedIn()){
return false;
}
return true;
}
<强> MainActivity.java 强>
boolean b = session.checkLogin();
if(!b) {
Intent i = new Intent(MainActivity.this, LoginActivity.class);
...
_context.startActivityForResult(i, 1);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// Check which request we're responding to
if (requestCode == 1) {
// Make sure the request was successful
if (resultCode == RESULT_OK) {
// Do something
}
}
}
答案 1 :(得分:0)
if(!this.isLoggedIn()){
Intent i = new Intent(_context, LoginActivity.class);
i.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
_context.startActivity(i);
((活动)_context).finish();
}