I have no idea, which factor may cause this problem, as even after debugging, still cannot figure it out. The problem is, that after start of application, I am trying to fill up text fields for login and password with saved data from previous login. However I cannot get reference to TextFields :/ It always get's null, and dunno why. What I have so far is.
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);
}
}
}
Does anybody know, why do I always get userName and password variables as null in loadPreferences method? In loginClick method it works fine :/ ...
Thanks in advance