This question already has answers here:
What is a NullPointerException, and how do I fix it?
(12 answers)
Closed 5 years ago.
When compiling my code I'm get this error
java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()'
and I still can't figure why. For these error My App got crash. Please help on this I'm not able to find the what exact mistake i did.
Caused by: java.lang.NullPointerException: Attempt to invoke interface method 'android.content.SharedPreferences$Editor android.content.SharedPreferences.edit()' on a null object reference
at com.chatbook.loki.chatbook.SessionManger.<init>(SessionManger.java:16)
at com.chatbook.loki.chatbook.Login.onCreate(Login.java:44)
at android.app.Activity.performCreate(Activity.java:6001)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2261)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2368)
Here is my code
SessionManager.java
public class SessionManger {
SharedPreferences pref;
SharedPreferences.Editor editor = pref.edit();
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "exmaple";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_EMAIL = "email";
public static final String KEY_PASSWORD = "password";
public SessionManger(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);
editor = pref.edit();
editor.apply();
}
public void createLoginSession(String email, String password){
editor.putBoolean(IS_LOGIN,true);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_PASSWORD, password);
editor.commit();
}
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN,true);
}
public void checkLogin(){
if(!this.isLoggedIn()){
Intent in = new Intent(_context,Login.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(in);
}
}
public HashMap<String, String> getUserDetails(){
HashMap<String,String> user = new HashMap<String, String>();
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
return user;
}
public void logout(){
editor.clear();
editor.commit();
Intent in = new Intent(_context,Login.class);
in.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
in.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(in);
}
}
and my Login activity file
public class Login extends AppCompatActivity {
Button b1,b2,b3;
EditText e1,e2;
FirebaseAuth auth;
ProgressBar pgbar;
SessionManger session;
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
auth = FirebaseAuth.getInstance();
b1 = (Button) findViewById(R.id.logBsign);
b2 = (Button) findViewById(R.id.logBlog);
b3 = (Button) findViewById(R.id.logBreset);
e1 = (EditText) findViewById(R.id.logeditemail);
e2 = (EditText) findViewById(R.id.logeditpass);
pgbar = (ProgressBar) findViewById(R.id.progressBar);
session = new SessionManger(context.getApplicationContext());
}
public void signup(View v) {
Intent in = new Intent(Login.this, SignUp.class);
startActivity(in);
}
public void login(View v){
/*Intent in = new Intent(Login.this, MainActivity.class);
startActivity(in);*/
final String email = e1.getText().toString().trim();
final String password = e2.getText().toString().trim();
if(TextUtils.isEmpty(email)){
Toast.makeText(getApplicationContext(),"Enter email address", Toast.LENGTH_SHORT).show();
return;
}
if(TextUtils.isEmpty(password)){
Toast.makeText(getApplicationContext(), "Enter password", Toast.LENGTH_SHORT).show();
return;
}
pgbar.setVisibility(View.VISIBLE);
auth.signInWithEmailAndPassword(email,password).addOnCompleteListener(Login.this, new OnCompleteListener<AuthResult>() {
#Override
public void onComplete(#NonNull Task<AuthResult> task) {
pgbar.setVisibility(View.GONE);
if(!task.isSuccessful()){
if(password.length() < 6){
e1.setError(getString(R.string.minimum_password));
}else{
Toast.makeText(Login.this, getString(R.string.auth_failed),Toast.LENGTH_SHORT).show();
}
}else{
session.createLoginSession(email, password);
Intent in = new Intent(Login.this, MainActivity.class);
startActivity(in);
//finish();
}
}
});
}
public void reset(View v){
Intent in = new Intent(Login.this, ResetPassword.class);
startActivity(in);
}
//To exit from App
public boolean exit = false;
#Override
public void onBackPressed() {
if(exit){
finish();;
}
else{
Toast.makeText(this,"Press again to exit",Toast.LENGTH_SHORT).show();
exit = true;
}
}
}
Do not
session = new SessionManger(context.getApplicationContext());
Do
session = new SessionManger(Login.this);
And
SharedPreferences pref;
Editor editor;
Context _context;
int PRIVATE_MODE = 0;
Then
// Constructor
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
Shared preferences can be edited like below ,pass correct name
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
Editor editor = sharedPreferences.edit();
editor.putString(key, name);
editor.apply();
SharedPreferences sharedPreferences = PreferenceManager
.getDefaultSharedPreferences(this);
String name = sharedPreferences.getString(key, "default value");
fix the global variable cause null
SharedPreferences.Editor editor = pref.edit(); <-- wrong pref cause null
SharedPreferences.Editor editor; <-- just this
Optional
#SuppressLint("CommitPrefEdits")
public SessionManger(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME,PRIVATE_MODE);
editor = pref.edit();
//editor.apply(); //no need for apply
}
It's because your pref is null...
SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", MODE_PRIVATE);
SharedPreferences pref;
SharedPreferences.Editor editor = pref.edit();
pref is null;
Related
I implemented code for Session Management day before yesterday It's working properly but now It's giving an error on getString() please check this below code and tell me where I am wrong.
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "MaangalPref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_PASSWORD = "name";
public static final String KEY_EMAIL = "email";
public static final String KEY_GENDER = "gender";
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public SessionManager() {}
Create login session
public void createLoginSession(String name, String email, String gender){
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_PASSWORD, name);
editor.putString(KEY_EMAIL, email);
editor.putString(KEY_GENDER, gender);
editor.commit();
}
Check login method wil check user login status
If false it will redirect user to login page Else won't do anything
public void checkLogin(){
if(!this.isLoggedIn()){
Intent i = new Intent(_context, AuthenticActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
Get stored session data
public HashMap<String, String> getUserDetails(){
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_PASSWORD, pref.getString(KEY_PASSWORD, null));
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
user.put(KEY_GENDER,pref.getString(KEY_GENDER, null));
return user;
}
public void logoutUser(){
editor.clear();
editor.commit();
Intent i = new Intent(_context, AuthenticActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
public boolean isLoggedIn(){
return pref.getBoolean(IS_LOGIN, false);
}
}
onCreate method of Fragment Class
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session class instance
session = new SessionManager();
// get user data from session
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
Log.e("email________NewMatches",email);
DATA_URL = "http://192.168.2.110/xp/new_matches.php?matri_id="+email;
Log.e("URL________NewMatches",DATA_URL);
}
I think your are calling empty constructor of SessionManager in tab fragment like
session = new SessionManager();
that you are declared in your SessionManager class
public SessionManager(Context context){
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public SessionManager() {}
so finally use in tab fragment
session = new SessionManager(getContext()); //give context of class
instead of session = new SessionManager();
use below code
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Session class instance
session = new SessionManager(getContext()); //here you have to change
// get user data from session
HashMap<String, String> user = session.getUserDetails();
email = user.get(SessionManager.KEY_EMAIL);
Log.e("email________NewMatches",email);
DATA_URL = "http://192.168.2.110/xp/new_matches.php?matri_id="+email;
Log.e("URL________NewMatches",DATA_URL);
}
and also remove empty constructor from SessionManager for future purpose....
I'm storing data on sharedpreferences when user is logged in and setting it in a textview. I want to remove one specific data when user logged out. The problem is data is being stored but not removing. I have tried below code.
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context _context;
int PRIVATE_MODE = 0;
private static final String PREF_NAME = "NaafcoPref";
private static final String IS_LOGIN = "IsLoggedIn";
public static final String KEY_ID = "id";
public static final String KEY_RESULT = "result";
public static final String SCAN_RESULT = "s_result";
public SessionManager(Context context) {
this._context = context;
pref = _context.getSharedPreferences(PREF_NAME, PRIVATE_MODE);
editor = pref.edit();
}
public void createLoginSession(String id) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_ID, id);
editor.commit();
}
public void getResult(String result) {
editor.putBoolean(IS_LOGIN, true);
editor.putString(KEY_RESULT, result);
editor.commit();
}
public void getScanResult(String scanResult) {
editor.putString(SCAN_RESULT, scanResult);
editor.commit();
}
public void checkLogin() {
if (!this.isLoggedIn()) {
Intent i = new Intent(_context, PointActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
}
public HashMap<String, String> getUserDetails() {
HashMap<String, String> user = new HashMap<String, String>();
user.put(KEY_ID, pref.getString(KEY_ID, null));
user.put(KEY_RESULT, pref.getString(KEY_RESULT, null));
user.put(SCAN_RESULT, pref.getString(SCAN_RESULT, null));
return user;
}
public void logoutUser() {
editor.remove(SCAN_RESULT).clear().commit();
//editor.clear();
//editor.commit();
Intent i = new Intent(_context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
public boolean isLoggedIn() {
return pref.getBoolean(IS_LOGIN, false);
}
}
To remove specific values: SharedPreferences.Editor.remove() followed by a commit()
To remove them all SharedPreferences.Editor.clear() followed by a commit()
If you don't care about the return value and you're using this from your application's main thread, consider using apply() instead.
Try this,
public void logoutUser() {
SharedPreferences sp = Preferences.getInstance().pref;
SharedPreferences.Editor editor = sp.edit();
editor.remove(SCAN_RESULT);
editor.commit();
Intent i = new Intent(_context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
You need to reinitialize sharepreference editer to clear data
public void logoutUser() {
editor = pref.edit();
editor.clear().commit();
Intent i = new Intent(_context, MainActivity.class);
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
_context.startActivity(i);
}
Try implementing this for logout:
/**
* Clear All user preferences
* Use this when user logs out
*/
public void clearPreferences() {
isLoggedIn(); // this will set the login as false
pref.edit().clear().apply();
//also clear from default preferences
SharedPreferences defaultPref = PreferenceManager.getDefaultSharedPreferences(context);
defaultPref.edit().clear().apply();
}
First you need to create singleton class like PreferenceManager
private PreferenceManager(Context ctx) {
prefs = ctx.getApplicationContext().getSharedPreferences(ctx.getPackageName(), Context.MODE_PRIVATE);
editor = prefs.edit();
}
public static PreferenceManager getInstance(Context ctx) {
if (sInstance == null) {
sInstance = new PreferenceManager(ctx);
}
return sInstance;
}
You can use below snippet
public void clearPreference() {
editor.remove("your preference key that you want to clear");
.
.add all preference key that you want to clear
.
editor.commit();
}
SharedPreferences pref = context.getSharedPreferences("s_result", Context.MODE_PRIVATE);
pref.edit().clear().commit();
I am facing issues in relaunch of android app, i;e
When I run my application first activity is login, on login it will navigate to home page.
Suppose if home button is pressed and relaunch application from app-drawer it will again start from login page. But if open it from running list in task manager it will come with login page. How to resolve this issue please help me.
add this code to your Login.java OnCreate
if (!TextUtils.isEmpty(SessionManager.isLogIn(Login.this))) {
SessionManager.user_id = SessionManager.isLogIn(Login.this);
SP = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
SessionManager.user_name = SP.getString("user_name", "");
SessionManager.user_name = SP.getString("user_pass", "");
SessionManager.uId = SP.getString("id", "");
Intent intent = new Intent(Login.this, HomePage.class);
startActivity(intent);
finish();
}
Make a java class name it SessionManager.java
public class SessionManager {
static Context context;
public static String PREFS_NAME = "settings";
static SharedPreferences preferences;
public static String user_id ="";
public static String user_name ="";
public static String user_pass ="";
public static boolean check = true;
#Override
public void onCreate() {
super.onCreate();
context = getApplicationContext();
mInstance = this;
}
public static String isLogIn(Context context) {
preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
user_name = preferences.getString("user_name", "");
user_pass = preferences.getString("user_pass", "");
return user_id = preferences.getString("user_id", "");
}
public boolean logoutUser(Context context) {
preferences = context.getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
return true;
}
}
Add this code in onClick of LOGIN in Login.java
SP = getSharedPreferences(PREFS_NAME, MODE_PRIVATE);
Editor editit = SP.edit();
editit.putString("user_id", user_id);
editit.putString("user_name", etUsername.getText().toString());
editit.putString("user_pass", etPassword.getText().toString());
editit.commit();
SessionManager.user_id = user_id;
SessionManager.user_name = etUsername.getText().toString());
SessionManager.user_pass = etPassword.getText().toString());
Make this Global in Login.java
SharedPreferences SP;
I have 2 activities namely MainActivity and OKActivity. The MainActivity statically checks for a password and lets you go to the OKActivity. I have used SharedPrefrences in the OKActivity for changing the password to a new one.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final EditText password = (EditText) findViewById(R.id.editText_Password);
Button enter = (Button) findViewById(R.id.button);
enter.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
String user_pass;
user_pass = password.getText().toString();
if (user_pass.isEmpty()) {
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setIcon(R.drawable.ic_launcher);
dialogBuilder.setTitle("Oops!");
dialogBuilder.setMessage("Password Field Cannot Be Empty");
dialogBuilder.setPositiveButton("OK", null);
dialogBuilder.show();
}
else
if (user_pass.equals("123")) {
Toast.makeText(MainActivity.this, "Welcome!", Toast.LENGTH_SHORT).show();
Intent I = new Intent("com.mavenmaverick.password.OKActivity");
startActivity(I);
}
else
if(user_pass != ("123")){
Toast.makeText(MainActivity.this, "Incorrect", Toast.LENGTH_SHORT).show();
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(MainActivity.this);
dialogBuilder.setIcon(R.drawable.ic_launcher);
dialogBuilder.setTitle("Oops!");
dialogBuilder.setMessage("Incorrect Password");
dialogBuilder.setPositiveButton("OK", null);
dialogBuilder.show();
}
}
});
}
public class OKActivity extends Activity {
EditText newPassword;
String newUserPassword;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_ok);
newPassword = (EditText) findViewById(R.id.new_password);
newUserPassword = newPassword.getText().toString();
getpasswordSharedPreferences();
Button changePassword = (Button) findViewById(R.id.button_change);
changePassword.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
newUserPassword = newPassword.getText().toString();
getpasswordSharedPreferences();
setSharedPreferences();
}
});
}
private String getpasswordSharedPreferences() {
SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
String password = userPassword.getString("THE_PASSWORD", "123");
return password;
}
private void setSharedPreferences() {
SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
SharedPreferences.Editor password_edior = userPassword.edit();
password_edior.putString("THE_PASSWORD", newUserPassword);
password_edior.commit();
Toast.makeText(OKActivity.this, "Password Change Succesful", Toast.LENGTH_SHORT).show();
AlertDialog.Builder dialogBuilder = new AlertDialog.Builder(OKActivity.this);
dialogBuilder.setIcon(R.drawable.ic_launcher);
dialogBuilder.setTitle("Done!");
dialogBuilder.setMessage("New Password : "+newUserPassword);
dialogBuilder.setPositiveButton("OK", null);
dialogBuilder.show();
}
How can I access the SharedPrefrences in OKActivity for the password and use it in my MainActivity to allow access thereby making things dynamic over user-interaction cycles.
Just access the SharedPreferences in your OKActivity and in your MainActivity. The trick is to use the same TAG name - in your case it's 'USER_PASSWORD'.
Have a look at this --> SharedPreferences
Create a SharedPrefrences.java //then we can use when ever we need
public class SharedPrefrences {
public static void saveData(String name, String value, Context context) {
try {
SharedPreferences settings = context
.getSharedPreferences(Configuration.getPrefsName(), 0);
SharedPreferences.Editor editor = settings.edit();
editor.putString(name, value);
editor.commit();
} catch (NullPointerException ignored) {
}
}
public static String getData(String name, Context context) {
try {
SharedPreferences settings = context
.getSharedPreferences(Configuration.getPrefsName(), 0);
return settings.getString(name, "");
} catch (NullPointerException ignored) {
return "";
}
}
}
//In MainActivity
SharedPrefrences.saveData("Password","123456", getApplicationContext());
//In OKActivity
String passwordfromMainActivty = PreferencesUtils.getData("Password", getApplicationContext());
//To Add Newpassword
SharedPrefrences.saveData("NewPassword","abcd", getApplicationContext());
You get the same way in both activity's. Do get of your SharedPreferences with the same TAG.
private String getpasswordSharedPreferences() {
SharedPreferences userPassword = getSharedPreferences("USER_PASSWORD", MODE_PRIVATE);
String password = userPassword.getString("THE_PASSWORD", "123");
return password;
}
Maybe you can put this method's in other class, and call when you want from all activities:
You can put your set method here too
For example:
public class SharedPrefs {
private static final String SHARED_PREF = "USER_PASSWORD";
private static final String KEY_PASSWORD = "THE_PASSWORD";
public static void getStoredSharedPref(Context context, String key, String value) {
SharedPreferences sharedPref = context.getSharedPreferences(SHARED_PREF, Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putString(propertyKey, value);
editor.commit();
}
}
and then call in your activities
SharedPrefs.getStoredSharedPref(context, SharedPrefsUtils.KEY_PASSWORD,"1234");
I'm trying to use sharedpreferences to check whether the user logged in before they start using the app. I save the username in shredpreferences when user log in.
Login.java
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);
Button btnLogin = (Button) findViewById(R.id.buttonlogin);
btnLogin.setOnClickListener(new View.OnClickListener() {
public void onClick(View adapt) {
EditText usernameEditText = (EditText) findViewById(R.id.EditUserName);
userName = usernameEditText.getText().toString();
EditText passwordEditText = (EditText) findViewById(R.id.EditPassword);
userPassword = passwordEditText.getText().toString();
if (userName.matches("") && userPassword.matches("")) {
toast("Please enter Username and Password");
return;
} else if (userName.matches("") || userName.equals("")) {
toast("Please enter Username");
return;
} else if (userPassword.matches("") || userPassword.equals("")) {
toast("Please enter Password");
return;
} else {
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.putString("MEM1", userName);
editor.commit();
new DownloadFilesTask().execute();
}
}
});
}
private void toast(String text) {
Toast.makeText(this, text, Toast.LENGTH_SHORT).show();
}
private class DownloadFilesTask extends AsyncTask<Void, Void, Void> {
protected void onPreExecute() {
}
protected void onPostExecute(Void result) {
toast("user logged in");
startActivity(new Intent(Login.this, MainActivity.class));
finish();
}
#Override
protected Void doInBackground(Void... params) {
return null;
}
}
}
and I tried to check the username value before I start my mainactivity.
SharedPreferences sharedPreferences = getPreferences(MODE_PRIVATE);
String username =sharedPreferences.getString("MEM1", "");
if(username.equalsIgnoreCase("")||username.length()==0)
{
toast("username is null");
startActivity(new Intent(MainActivity.this, Login.class));
finish();
}
but the username is always null. Please help. Thanks
Calling getPreferences means that you have to be in the same activity. Using getSharedPreferences allows you to share the preferences between activities. You just have to define a name for the preferences when calling getSharedPreferences("Pref name here", MODE_PRIVATE);
Write below Code instead of your code to save username into sharedpreferences.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
SharedPreferences.Editor editor = myPrefs.edit();
editor.putString("MEM1", userName);
editor.commit();
And Use below code For Get Preferences Value.
SharedPreferences myPrefs = this.getSharedPreferences("myPrefs", MODE_WORLD_READABLE);
String username = myPrefs.getString("MEM1","");
private final String TAXI_SPREF = "TAXI_SHARED_PREFERENCES";
//set-save data to shared preferences
SharedPreferences.Editor sPEditor = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).edit();
sPEditor.putInt("USERID", etUserID.getText().toString());
sPEditor.putString("EMAIL", etEmail.getText().toString());
sPEditor.apply();
//get data from shared preferences
SharedPreferences sharedPreferences = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE);
int userID = sharedPreferences.getInt("USERID", 0);
string email = sharedPreferences.getString("EMAIL", 0);
//////// or /////
String email = getSharedPreferences(TAXI_SPREF, MODE_PRIVATE).getString("EMAIL","EMPTY");
if (!email.equals("EMPTY")){
etEmail.setText(email);
}