登录后显示空白页面,而不是正确的布局.安卓系统



当用户登录应用程序时,它会显示一个空白的白色屏幕,而不是正确的页面,当我关闭并重新打开应用程序时它会显示正确的布局。这就是我决定使用哪种布局的方式。我不确定这是否重要,但在manifest中,我将LoginActivity.java设置为常规活动,并将其放在MainActivity.java声明下面。下面的所有代码都在我下面提到的每个类的onCreate方法中。

<activity
android:name=".activities.LoginActivity"
android:label="Login"
android:theme="@style/AppTheme">
</activity>
//MainActivity.java. The user gets saved to SharedPreferences when they 
//login, here it checks if they are logged in to see which layout gets 
//displayed.
User user = SharedPreferencesHelper.getUser(MainActivity.this);
if (user.getToken() == null) {
Intent login = new Intent(MainActivity.this, LoginActivity.class);
startActivity(login);
} else {
setContentView(R.layout.activity_main);
buildMain();
}
//LoginActivity.java. Here I send the username, password to the server to 
//login and receive the user token (api authentication), if the response is
//successful I save all the user stuff to SharedPreferences and check to see 
//if it was saved in order to finish the activity.
if (response.isSuccessful()) {
User user = response.body();
SharedPreferencesHelper.setUser(LoginActivity.this, user);
User current = SharedPreferencesHelper.getUser(LoginActivity.this);
if (current.getToken() != null) {
finish();
}
}

这些只是发生操作的代码片段,实际的代码要长得多,但我不认为其他东西是超级相关的。

如果您正在使用启动屏幕,您应该检查用户是否已登录到启动屏幕。

例如,在启动屏幕的onCreate方法上放置以下代码

User user = SharedPreferencesHelper.getUser(SplashScreen.this);    
if (user.getToken() == null) {
Intent login = new Intent(SplashScreen.this, LoginActivity.class);
startActivity(login);
} else {
Intent mainActivity = new Intent(SplashScreen.this, MainActivity.class);
startActivity(mainActivity);
finish();  // To remove splash screen from the backstack
}

然后在登录活动

if (response.isSuccessful()) {
User user = response.body();
SharedPreferencesHelper.setUser(LoginActivity.this, user);
User current = SharedPreferencesHelper.getUser(LoginActivity.this);
if (current.getToken() != null) {
Intent mainActivity = new Intent(LoginActivity.this, MainActivity.class);
startActivity(mainActivity);
finish();  // To remove login from the backstack
}
}

最新更新