로그인 정보를 공유 환경 설정에 저장하는 작은 로그인 응용 프로그램을 만들려고했습니다.공유 환경 설정 컨텍스트 오류
내가 공유 환경 설정 데이터를 저장하고 가져오고 지우는 클래스 인 Mysession을 만들었습니다. 클래스가 컨텍스트를 취하고 로그인 컨텍스트에서 로그인 클래스로 컨텍스트를 전달한 다음 데이터를 공유 환경 설정에 저장합니다. 오류.
오류는 내가 빈 컨텍스트를 통과했다는 것을 나타냅니다.
이들은 내 수업 및 활동입니다.
로그인 활동
public class LoginActivity extends AppCompatActivity {
EditText lemailtxt,lpasstxt;
Button loginbttn;
String lurl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
lurl = "http://192.168.1.6/test/test.php";
lemailtxt = (EditText) findViewById(R.id.emailtxt);
lpasstxt = (EditText) findViewById(R.id.passtxt);
loginbttn = (Button) findViewById(R.id.loginbttn);
loginbttn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
MLoginOnline login = new MLoginOnline(getApplicationContext()
,lurl,lemailtxt.getText().toString(),lpasstxt.getText().toString());
login.execute();
}
});
}
로그인 클래스
public class MLoginOnline extends AsyncTask<Void,Void,String>{
Context mContext;
String Purl;
String Pemail,Ppass;
ProgressDialog progressDialog;
public MLoginOnline(Context mContext, String purl, String pemail, String ppass) {
this.mContext = mContext;
Purl = purl;
Pemail = pemail;
Ppass = ppass;
}
MySession session = new MySession(mContext);
@Override
protected void onPreExecute() {
super.onPreExecute();
progressDialog = new ProgressDialog(mContext);
progressDialog.setTitle("Login");
progressDialog.setMessage("Loging in please wait......");
progressDialog.show();
}
@Override
protected String doInBackground(Void... params) {
String data = Loginto();
return data;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
String id,name,sem;
if (s ==null){
Toast.makeText(mContext,"Error Login in",Toast.LENGTH_SHORT).show();
progressDialog.hide();
}else {
progressDialog.hide();
try {
JSONObject object = new JSONObject(s);
id = object.getString("id");
name = object.getString("name");
sem = object.getString("sem");
session.InPutUser(id,name,Pemail,sem);
Intent intent = new Intent(mContext, HomeActivity.class);
mContext.startActivity(intent);
} catch (JSONException e) {
e.printStackTrace();
}
}
}
private String Loginto(){
InputStream inputStream=null;
String line = null;
try {
URL url = new URL(Purl+"?Email="+Pemail+"&Password="+Ppass);
HttpURLConnection con = (HttpURLConnection) url.openConnection();
inputStream = new BufferedInputStream(con.getInputStream());
BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(inputStream));
StringBuffer stringBuffer = new StringBuffer();
if(bufferedReader != null){
while ((line=bufferedReader.readLine()) != null){
stringBuffer.append(line+"\n");
}
}else {
return null;
}
return stringBuffer.toString();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}
** 공유 환경 설정 클래스 **
public class MySession {
Context mcontext;
SharedPreferences preferences;
SharedPreferences.Editor editor;
int PRIVATE_MODE = 0;
private static final String PREFER_NAME = "session";
private final String IS_USER_LOGED_IN = "IsUserLogedIn";
public final String KEY_ID = "id";
public final String KEY_EMAIL = "email";
public static final String KEY_NAME = "name";
public static final String KEY_SEM = "sem";
public MySession(Context mcontext) {
this.mcontext = mcontext;
this.preferences = mcontext.getSharedPreferences(PREFER_NAME,PRIVATE_MODE);
editor = preferences.edit();
}
public void InPutUser(String id,String name,String email,String sem){
editor.putBoolean(IS_USER_LOGED_IN,true);
editor.putString(KEY_ID,id);
editor.putString(KEY_NAME,name);
editor.putString(KEY_SEM,sem);
editor.putString(KEY_EMAIL,email);
editor.commit();
}
public void logoutUser(Context context,Class intent){
// Clearing all user data from Shared Preferences
editor.clear();
editor.commit();
// After logout redirect user to Login Activity
Intent i = new Intent(context, intent);
// Closing all the Activities
i.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
// Add new Flag to start new Activity
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// Staring Login Activity
context.startActivity(i);
}
public HashMap<String, String> getNameAndSem(){
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, preferences.getString(KEY_NAME, null));
// user email id
user.put(KEY_EMAIL, preferences.getString(KEY_SEM, null));
// return user
return user;
}
public HashMap<String, String> getIDandEMail() {
HashMap<String, String> user = new HashMap<String, String>();
// user name
user.put(KEY_NAME, preferences.getString(KEY_ID, null));
// user email id
user.put(KEY_EMAIL, preferences.getString(KEY_EMAIL, null));
// return user
return user;
}
public boolean IsUserLoggedIn(){
return preferences.getBoolean(IS_USER_LOGED_IN, false);
}
}
고양이
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.zer0ll.demo.studentapp, PID: 20793
java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.SharedPreferences android.content.Context.getSharedPreferences(java.lang.String, int)' on a null object reference
at com.zer0ll.demo.studentapp.MySession.<init>(MySession.java:36)
at com.zer0ll.demo.studentapp.MLoginOnline.<init>(MLoginOnline.java:43)
at com.zer0ll.demo.studentapp.MainView.LoginActivity$1.onClick(LoginActivity.java:32)
at android.view.View.performClick(View.java:5156)
at android.view.View$PerformClick.run(View.java:20755)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5835)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1399)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1194)
Application terminated.
대단히 감사합니다. –
환영합니다 ... – rafsanahmad007