我不知道哪个因素可能导致此问题,因为即使在调试后,仍然无法弄清楚。问题是,在启动应用程序后,我试图用以前登录时保存的数据填充登录名和密码的文本字段。但是我无法获得对TextFields的引用:/它总是为null,为什么不呢?到目前为止,我所拥有的是。
public class Login extends ActionBarActivity {
private String SHARED_PREFS_KEY = "SBJP";
private String LOGIN_KEY = "login";
private String PASS_KEY = "pass";
private EditText userName;
private EditText password;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
PlaceholderFragment phFrag = new PlaceholderFragment();
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, phFrag)
.commit();
}
loadPreferences(phFrag);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.login, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
return rootView;
}
}
/**
* Handles login action
*/
public void loginClick(View v){
this.userName = (EditText) findViewById(R.id.login_screen_tf_login);
this.password = (EditText) findViewById(R.id.login_screen_tf_password);
String userNameStr = userName.getText().toString();
String passStr = password.getText().toString();
//Log.d("SB", userNameStr+":"+passStr);
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
Editor editor = prefs.edit();
editor.putString(PASS_KEY, passStr);
editor.putString(LOGIN_KEY, userNameStr);
editor.commit();
}
/**
* Handles creating new account
*/
public void createNewAccountClick(View v){
Log.d("SB", "Here comes account creation");
}
private void loadPreferences(PlaceholderFragment rootView){
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
String loginStr = prefs.getString(LOGIN_KEY, null);
String passStr = prefs.getString(PASS_KEY, null);
this.userName = (EditText) rootView.getView().findViewById(R.id.login_screen_tf_login);
this.password = (EditText) rootView.getView().findViewById(R.id.login_screen_tf_password);
Log.d("SB","Login/sn:"+loginStr+passStr);
if(loginStr != null && userName != null){
userName.setText(loginStr);
}
if(passStr != null && password != null){
password.setText(passStr);
}
}
}
有人知道吗,为什么在loadPreferences方法中总是将userName和password变量获取为null?在loginClick方法中,它可以正常工作:/ ...
提前致谢
最佳答案
this.userName = (EditText) findViewById(R.id.login_screen_tf_login);
this.password = (EditText) findViewById(R.id.login_screen_tf_password);
在这里,您需要获取片段的根视图,但要在PlaceholderFragment类的代码内:
userName = (EditText) getView().findViewById(R.id.login_screen_tf_login);
password = (EditText) getView().findViewById(R.id.login_screen_tf_password);
http://developer.android.com/reference/android/app/Fragment.html#getView()
当然,我们建议login_screen_tf_login和login_screen_tf_login EditText-s位于fragment_login布局中。
加成:
您可以按照以下方式重新设计PlaceholderFragment:
public static class PlaceholderFragment extends Fragment {
private String login;
private String password;
public void setAuthData(String login, String password){ this.login = login; this.password = password; }
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_login, container, false);
EditText userName = (EditText) rootView.findViewById(R.id.login_screen_tf_login);
userName.setText(login);
EditText passwordET = (EditText) rootView.findViewById(R.id.login_screen_tf_password);
passwordET.setText(password);
return rootView;
}
}
并使用PlaceholderFragment类,如下所示:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
SharedPreferences prefs = getSharedPreferences(SHARED_PREFS_KEY, MODE_PRIVATE);
String login = prefs.getString(LOGIN_KEY, null);
String password = prefs.getString(PASS_KEY, null);
PlaceholderFragment phFrag = new PlaceholderFragment();
phFrag.setAuthData(login, password);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, phFrag)
.commit();
}
}