我正在登录界面,我必须输入电子邮件ID和密码。登录成功后,我想在共享首选项中存储电子邮件ID,以便下次我想登录时,我不必输入电子邮件Id。我正在使用SessionManager类来执行此操作。
1.SessionMAnager.java
public class SessionManager {
SharedPreferences pref;
SharedPreferences.Editor editor;
Context context;
//Shared Preferences file name
private static final String PREFERENCE_NAME = "SessionManager";
// Shared Preferences Keys
private static final String IS_USER_LOGIN = "IsUserLoggedIn";
//Email Address
public static final String KEY_EMAIL = "email";
//Constructor
public SessionManager(Context context) {
this.context = context;
pref = context.getSharedPreferences(PREFERENCE_NAME, Context.MODE_MULTI_PROCESS);
editor = pref.edit();
}
//Create Login session
public void createUserLoginSession(String email) {
// Storing login value as TRUE
editor.putBoolean(IS_USER_LOGIN, true);
//Storing email in pref
editor.putString(KEY_EMAIL, email);
//editor.putBoolean(IS_USER_LOGIN,true);
editor.commit();
}
/**
* Check login method will check user login status
* If false it will redirect user to login page
* Else do anything
*/
public boolean checkLogin() {
// Check login status
if (!this.isUserLoggedIn()) {
// user is not logged in redirect him to Login Activity
Intent i = new Intent(context, Activity_LogInScreen.class);
// Staring Login Activity
context.startActivity(i);
return true;
}
return false;
}
/**
* Get stored session data
*/
public HashMap<String, String> getUserDetails() {
//Use hashmap to store user credentials
HashMap<String, String> user = new HashMap<String, String>();
// user email id
user.put(KEY_EMAIL, pref.getString(KEY_EMAIL, null));
// return user
return user;
}
/**
* Clear session details
*/
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(context, Activity_LogInScreen.class);
// Staring Login Activity
context.startActivity(i);
}
// Check for login
public boolean isUserLoggedIn() {
return pref.getBoolean(IS_USER_LOGIN, false);
}
}
2.Activity_LoginScreen.java
public class Activity_LogInScreen extends Activity implements OnClickListener {
private EditText editText;
private EditText editTextpassword;
private Button btn_SignUp;
private Button btn_LogIn;
public String get_User, get_Password;
private CheckConnectionInternet obj;
private TransparentProgressDialog dialog;
private SharedPreferences prefs;
JSONObject jsonObj;
public Model_LoginDetails objLoginDetails;
private ArrayList<Model_LoginDetails> arr_LogInUserDetails = new ArrayList<Model_LoginDetails>();
AlmabayDatabase almabayDatabase;
SQLiteDatabase sqLiteDatabase;
//SessionManager class
SessionManager sessionManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_loginscreen);
sessionManager = new SessionManager(this);
dialog = new TransparentProgressDialog(Activity_LogInScreen.this, R.drawable.loader);
prefs = this.getSharedPreferences(Prefs_Registration.pacakgename, Context.MODE_PRIVATE);
obj = new CheckConnectionInternet();
almabayDatabase = new AlmabayDatabase(this);
editText = (EditText) findViewById(R.id.edt_useremailId);
editTextpassword = (EditText) findViewById(R.id.edt_userpassword);
editText.getBackground().setColorFilter(Color.parseColor("#ffffff"),
PorterDuff.Mode.SRC_ATOP);
editTextpassword.getBackground().setColorFilter(
Color.parseColor("#ffffff"), PorterDuff.Mode.SRC_ATOP);
// Checking whether database contains the emailID or not
/* if (almabayDatabase.getEmailFromAccessTAble() != null) {
get_User = almabayDatabase.getEmailFromAccessTAble();
editText.setText(get_User);
}*/
Boolean userLoogein = sessionManager.isUserLoggedIn();
Log.e("IslOOgedIn", String.valueOf(userLoogein));
// get user data from session
HashMap<String, String> user = sessionManager.getUserDetails();
// get email
String email = user.get(SessionManager.KEY_EMAIL);
Log.e("email",email);
btn_SignUp = (Button) findViewById(R.id.btn_SignUp);
btn_LogIn = (Button) findViewById(R.id.btn_login);
btn_SignUp.setOnClickListener(this);
btn_LogIn.setOnClickListener(this);
}
@Override
public void onClick(View arg0) {
switch (arg0.getId()) {
case R.id.btn_login:
get_User = editText.getText().toString();
get_Password = editTextpassword.getText().toString();
if ((get_User.equals("")) || (get_Password.equals(""))) {
Toast.makeText(Activity_LogInScreen.this, "Fill Empty Field", Toast.LENGTH_LONG).show();
} else {
if (obj.isOnline(Activity_LogInScreen.this) == true) {
JARLogIn jar_Login = new JARLogIn(Activity_LogInScreen.this) {
private String status_Login;
@Override
public void result(String object) {
Log.e("Respose", object);
dialog.show();
try {
jsonObj = new JSONObject(object);
status_Login = jsonObj.getString("statusCode");
} catch (JSONException e) {
e.printStackTrace();
}
if (status_Login.equals("200")) {
String user_Id;
String access_token;
try {
JSONObject detailsObject = jsonObj.getJSONObject("detail");
String get_Id = detailsObject.getString("id");
String get_AccsToken = detailsObject.getString("access_token");
String userId = detailsObject.getString("user_id");
JSONObject jsonUserInfo = detailsObject.getJSONObject("userInfo");
JSONObject jsonuser = jsonUserInfo.getJSONObject("userInfo");
//user
JSONObject Json_user = jsonuser.getJSONObject("user");
String str_thumbnail_url = Json_user.getString("thumbnail_url");
String str_Name = Json_user.getString("name");
String str_userSkills = Json_user.getString("userSkills");
//create user's login session
sessionManager.createUserLoginSession(get_User);
// Delete the existing entries of AccessToken table
almabayDatabase.deleteAllFromAccessTable();
// Inserting UserID,emailID and access token in the database
almabayDatabase.insertIntoAccessTable(Integer.parseInt(get_Id), get_User, get_AccsToken);
objLoginDetails = new Model_LoginDetails(detailsObject.getString("id"), detailsObject.getString("user_id"), detailsObject.getString("access_token"), str_thumbnail_url, str_Name, str_userSkills);
arr_LogInUserDetails.add(objLoginDetails);
save_ValueSession(userId, get_AccsToken, get_User, str_Name, arr_LogInUserDetails, str_thumbnail_url);
fun_empty_EditTextFields();
} catch (JSONException e) {
e.printStackTrace();
}
} else {
fun_empty_EditTextFields();
Toast.makeText(Activity_LogInScreen.this, "User not found", Toast.LENGTH_LONG).show();
}
}
private void fun_empty_EditTextFields() {
dialog.cancel();
editText.setText("");
editTextpassword.setText("");
}
};
jar_Login.execute(get_User, get_Password);
} else {
Toast.makeText(Activity_LogInScreen.this,
"Connection Error", Toast.LENGTH_LONG).show();
}
}
break;
case R.id.btn_SignUp:
fun_Intent(Activity_Registeration.class);
break;
default:
break;
}
}
在Activity_LoginScreen中,我使用会话管理器类
将emailID存储在共享首选项中//create user's login session
sessionManager.createUserLoginSession(get_User);
我在这里得到NullPointerException。我知道我在将值添加到它之前访问共享首选项。但是如果我使用数据库它正在为我工作。请帮我解决这个问题。
答案 0 :(得分:-1)
在你的情况下出现的问题可能是JSONObject
可能正在处理null的事情。
如果你提到你的堆栈跟踪,可以说更多。