使用sharedpreferences存储用户名并在片段类中显示用户名

时间:2014-02-01 14:06:00

标签: android login android-fragments fragment sharedpreferences

我的应用程序有一个登录屏幕,它将用户发送到一个活动片段,我想知道如何使用共享首选项这样我就可以存储用户在登录活动中输入的用户名,并调用存储在另一个上的数据其他片段,因此用户名始终显示在每个其他片段活动上。

我可以传递一个intent,它存储来自登录类的用户输入并将其传递给所有片段。但是我的应用程序就像这样工作,用户只需要在下次关闭时登录,它将绕过登录,并且意图将传递null,没有输入传递。 我尝试使用共享首选项,但它在myhomFragment类中返回nullPointer lblName.setText(sharedpreferences.getString(name, ""));

我存储登录类中的数据:

public static final String MyPREFERENCES = "MyPrefs";
public static final String name = "nameKey";
public static final String pass = "passwordKey";
SharedPreferences sharedpreferences;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.login);

    // Importing all assets like buttons, text fields
    inputUser = (EditText) findViewById(R.id.loginUser);
    inputPassword = (EditText) findViewById(R.id.loginPassword);
    btnLogin = (Button) findViewById(R.id.btnLogin);
    btnLinkToRegister = (Button) findViewById(R.id.btnLinkToRegisterScreen);
    loginErrorMsg = (TextView) findViewById(R.id.login_error);

    // Login button Click Event
    btnLogin.setOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View view) {

            // Get username, password from EditText
            //String username = inputUser.getText().toString();
            //String password = inputPassword.getText().toString();
            //
            Editor editor = sharedpreferences.edit();
            String u = inputUser.getText().toString();
            String p = inputPassword.getText().toString();
            editor.putString(name, u);
            editor.putString(pass, p);
            editor.commit();
            Intent i = new Intent(getApplicationContext(),
                    MainActivity2.class);
            startActivity(i);
            new ConnectDb().execute();

        }

    });

    // Link to Register Screen
    btnLinkToRegister.setOnClickListener(new View.OnClickListener() {

        public void onClick(View view) {
            Intent i = new Intent(getApplicationContext(),
                    RegisterActivity.class);
            startActivity(i);
            finish();
        }
    });
}

//
@Override
protected void onResume() {
    sharedpreferences = getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);
    if (sharedpreferences.contains(name)) {
        if (sharedpreferences.contains(pass)) {
            Intent i = new Intent(getApplicationContext(),
                    MainActivity2.class);

            startActivity(i);
        }
    }
    super.onResume();
}

homeFragment类,我希望用户名出现在texView中(我希望能够显示登录到每个片段的用户)。

public static final String MyPREFERENCES = "MyPrefs";
public static final String name = "nameKey";
public static final String pass = "passwordKey";
TextView lblName;

AlertDialogManager alert = new AlertDialogManager();
// Session Manager Class
SharedPreferences sharedpreferences;

public HomeFragment() {
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {

    sharedpreferences = getActivity().getSharedPreferences(MyPREFERENCES,
            Context.MODE_PRIVATE);

    if (sharedpreferences.contains(name)) {
        lblName.setText(sharedpreferences.getString(name, ""));

    }

1 个答案:

答案 0 :(得分:0)

删除public HomeFragment() { } - 通常会避免片段和活动中的构造函数。由于您没有初始化lblName变量,您获得了NPE。