Android应用程序在启动时显示白色空白屏幕

时间:2017-02-10 08:13:25

标签: android sharedpreferences

我正在尝试实现一个简单的登录应用程序,其中使用共享首选项将用户详细信息(如用户名和密码)硬编码到其中。 当我尝试启动应用程序时,它会显示一个空白的白色屏幕,即使我清除应用程序缓存,也会无限期地评论我的评论。 我应该怎么解决它。 请帮助我。!这是我的启动活动

public class MainActivity extends AppCompatActivity {
BaseClass session;
EditText userName;
EditText passWord;
Button logIn;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    //  SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);
   session = new BaseClass(getBaseContext());
        // <---- run your one time code here
        //  BaseClass.getApp();
        userName = (EditText) findViewById(R.id.etusername);
        passWord = (EditText) findViewById(R.id.etpassword);
        logIn = (Button) findViewById(R.id.btnlogin);
    //toasting line
        Toast.makeText(getBaseContext(),"User Login Status: "+session.isUserLoggedIn(),Toast.LENGTH_LONG).show();
    session.checkLogin();
        logIn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                String user = userName.getText().toString().trim();
                String pass = passWord.getText().toString().trim();
                String userVal = session.pref.getString(KEY_USERNAME, "username");
                String passVal = session.pref.getString(KEY_PASSWORD, "password");
                //  userName.setText(userVal);
                // passWord.setText(passVal);
                Toast.makeText(getBaseContext(), "Editfield value:" + user, Toast.LENGTH_LONG).show();
                Toast.makeText(getBaseContext(), "Actual username:" + userVal, Toast.LENGTH_LONG).show();
                if (user.equals(userVal) && pass.equals(passVal)) {
                    session.createUserLoginSession(userVal,
                            passVal);
                    Intent intent = new Intent(MainActivity.this, QRScanning.class);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK);
                    intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);
                    startActivity(intent);

                } else {
                    Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
                    userName.setText("");
                    passWord.setText("");
                }
            }
        });
    }
}

Ans应用程序实例

public class BaseClass extends Application {
//private static BaseClass sInstance;
public static final String PREFER_NAME = "Userdetails";
private static final String IS_USER_LOGIN = "IsUserLoggedIn";
public static final String KEY_USERNAME = "username";
public static final String KEY_PASSWORD = "password";
public static final String firstTime = "jjj";
public SharedPreferences pref;
SharedPreferences.Editor editor;
Context mContext;
int PRIVATE_MODE = 0;
@Override
public void onCreate() {
    super.onCreate();
    BaseClass session = new BaseClass(getApplicationContext());
    if (!session.pref.getBoolean(firstTime, false)) {

        session.editor.putString(KEY_USERNAME, "retailer");
        session.editor.putString(KEY_PASSWORD, "beco123");

        // mark first time has runned.
        // SharedPreferences.Editor editor = pref.edit();
        session.editor.putBoolean("firstTime", true);
        session.editor.commit();
    }
}
public BaseClass(Context context) {
    this.mContext = context;
    pref = mContext.getSharedPreferences(PREFER_NAME, PRIVATE_MODE);
    editor = pref.edit();
}
public BaseClass() {
}
//Create login session
public void createUserLoginSession(String username, String password) {
    // Storing login value as TRUE
    editor.putBoolean(IS_USER_LOGIN, true);

    // Storing username in pref
    editor.putString(KEY_USERNAME, username);

    // Storing password in pref
    editor.putString(KEY_PASSWORD, password);

    // commit changes
    editor.commit();
}
public void checkLogin() {
    // Check login status
    if (!this.isUserLoggedIn()) {

        // user is not logged in redirect him to Login Activity
        Intent i = new Intent(mContext, MainActivity.class);

        // Closing all the Activities from stack
        i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        // Add new Flag to start new Activity
        i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);

        // Staring Login Activity
        mContext.startActivity(i);
    }
}
public void logoutUser() {

    // Clearing all user data from Shared Preferences
    editor.clear();
    editor.commit();
    // After logout redirect user to Login Activity
    Intent i = new Intent(mContext, MainActivity.class);
    // Closing all the Activities
    i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
    // Add new Flag to start new Activity
    i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
    // Staring Login Activity
    mContext.startActivity(i);
}
public boolean isUserLoggedIn() {
    return pref.getBoolean(IS_USER_LOGIN, false);
}

}

1 个答案:

答案 0 :(得分:3)

你要进入一个循环。

当您从session.checkLogin()内的onCreate方法拨打MainActivity时,由于用户未登录,您将重新开始MainActivity,然后再次展示Toast并再次拨打session.chechLogin(),依此类推。

您应该尝试修复代码流。

希望这有帮助

<强>更新

尝试以这种方式修复:

像这样改变checkLogin方法:

public boolean checkLogin() {
    // Check login status
    return(this.isUserLoggedIn());
}

然后,在您的MainActivity课程中执行以下操作:

public class MainActivity extends AppCompatActivity {
    BaseClass session;
    EditText userName;
    EditText passWord;
    Button logIn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        session = new BaseClass(getBaseContext());
        if (session.checkLogin()) {
            Intent intent = new Intent(MainActivity.this, QRScanning.class);
            startActivity(intent);
            finish();
        }

        setContentView(R.layout.activity_main);

        // <---- run your one time code here
        //  BaseClass.getApp();
        userName = (EditText) findViewById(R.id.etusername);
        passWord = (EditText) findViewById(R.id.etpassword);
        logIn = (Button) findViewById(R.id.btnlogin);
        //toasting line
        Toast.makeText(getBaseContext(),"User Login Status: "+session.isUserLoggedIn(),Toast.LENGTH_LONG).show();

        logIn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                 // YOUR CODE TO HANDLE LOGIN HERE
            } 

        });
    }
}