我的appliaction功能包含一个带有注销按钮的菜单,允许您从任何活动中注销。这一直很好。它曾用于注销应用程序的欢迎屏幕,但我最近更改了它以注销到设备的主屏幕。现在,当我退出并打开应用程序时,它会强行关闭。在第二次尝试启动应用程序时,它将正确启动。
在第一次尝试时,它似乎尝试加载用户注销时运行的Activity,尽管LogCat显示它启动了正确的Intent:
08-13 17:14:18.983: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=uk.ac.ic.doc.vmw10.wherewolf/.activities.Welcome }
08-13 17:14:19.053: VERBOSE/WWA(8611): setting up
08-13 17:14:19.064: DEBUG/AndroidRuntime(8611): Shutting down VM
08-13 17:14:19.064: WARN/dalvikvm(8611): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-13 17:14:19.083: ERROR/AndroidRuntime(8611): FATAL EXCEPTION: main
08-13 17:14:19.083: ERROR/AndroidRuntime(8611): java.lang.RuntimeException: Unable to resume activity {uk.ac.ic.doc.vmw10.wherewolf/uk.ac.ic.doc.vmw10.wherewolf.activities.Tabs}: java.lang.NullPointerException
在启动正确的Intent之后,会立即从错误的活动输出详细消息,然后应用程序因NullPointerException而崩溃。 为什么它没有启动欢迎活动,因为Intent声称它是?
这是退出功能:
public void logout() {
SharedPreferences.Editor prefEditor = preferences.edit();
prefEditor.remove(PASSWORD);
prefEditor.remove(FIRST_NAME);
prefEditor.remove(LAST_NAME);
prefEditor.remove(EMAIL_ADDRESS);
prefEditor.remove(DATE);
prefEditor.remove(PICTURE);
prefEditor.remove(TOKEN);
prefEditor.commit();
final Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
syncTask.cancel(true);
dbHelper.close();
dbHelper=null;
firstRun = true;
startActivity(intent);
}
编辑:以下是欢迎活动的onCreate方法:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(getApplicationContext());
Log.v(TAG, "preferences: "+preferences.getAll());
if(preferences.contains(WhereWolfActivity.EMAIL_ADDRESS)) {
Log.v(TAG, "user preferences saved from previous session: "+preferences.getAll().toString());
intent = new Intent(getApplicationContext(), Tabs.class);
startActivity(intent);
finish();
}
else if(preferences.contains(WhereWolfActivity.USER_ID) &! preferences.contains(WhereWolfActivity.EMAIL_ADDRESS)) {
Log.v(TAG, "known user returning to log in");
intent = new Intent(getApplicationContext(), Login.class);
startActivity(intent);
finish();
}
else {
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.welcome);
}
}
修改
它不仅启动了错误的Activity,它不会调用该Activity的onCreate方法,而是调用onStart。这表明它正试图从它停止的地方开始,即使这不是我想要它做的!我怎么能说服它不要这样做?
修改
由于我所有的'登录'Activity都有一个他们继承的超类,我在该超类的onStart()方法中添加了一些内容,用于检查用户是否已登录,如果没有,则会启动Welcome Activity: / p>
public void onStart () {
super.onStart();
if(!preferences.contains(EMAIL_ADDRESS)) {
Log.v(TAG, "not logged in");
Intent intent = new Intent(getApplicationContext(), Welcome.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
finish();
}
这没效果。这是LogCat输出:
08-13 20:41:10.223: INFO/ActivityManager(59): Starting activity: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] flg=0x10200000 cmp=uk.ac.ic.doc.vmw10.wherewolf/.activities.Welcome }
08-13 20:41:10.254: VERBOSE/WWA(2171): not logged in
08-13 20:41:10.263: INFO/ActivityManager(59): Starting activity: Intent { flg=0x4000000 cmp=uk.ac.ic.doc.vmw10.wherewolf/.activities.Welcome }
08-13 20:41:10.273: VERBOSE/WWA(2171): setting up
08-13 20:41:10.320: DEBUG/AndroidRuntime(2171): Shutting down VM
08-13 20:41:10.320: WARN/dalvikvm(2171): threadid=1: thread exiting with uncaught exception (group=0x4001d800)
08-13 20:41:10.343: ERROR/AndroidRuntime(2171): FATAL EXCEPTION: main
08-13 20:41:10.343: ERROR/AndroidRuntime(2171): java.lang.RuntimeException: Unable to resume activity {uk.ac.ic.doc.vmw10.wherewolf/uk.ac.ic.doc.vmw10.wherewolf.activities.Tabs}: java.lang.NullPointerException
超类有标签WWA。如您所见,它表示“未登录”,然后启动欢迎活动,然后继续进行。有什么想法吗?
答案 0 :(得分:0)
如果您要退回到主屏幕,它会告诉我您只想在家中启动应用程序时看一次欢迎屏幕?
如果这是正确的,那么在欢迎onCreate()中,在启动选项卡或登录活动之前调用finish()。现在你回家了 - >欢迎 - > tabs_or_login-> logout->再次启动应用程序
答案 1 :(得分:0)
我通过将其添加到我的超类的onPause()方法来解决问题:
if(!preferences.contains(EMAIL_ADDRESS)) { // this means the user has logged out
finish();
}
现在,当用户注销时,所有活动都已正确完成。我还将其更改为登录登录屏幕,而不是在阅读此帖后登录设备的主屏幕:Is quitting an application frowned upon?