gpt4 book ai didi

java - 由于 NullPointerException : Attempt to invoke virtual method HashMap on a null object reference 无法启动 Activity

转载 作者:行者123 更新时间:2023-12-02 06:53:44 26 4
gpt4 key购买 nike

我正在尝试启动 Activity checkin1。我正在使用 volley 并从 mySQL db 请求信息以显示在四个编辑文本字段中。
我正在使用我在我的应用程序的其他 Activity ( session 管理器)中使用的相同策略,并且我的代码是相同的,因为我一直在交叉引用并想知道为什么第 47 行会发生错误:
checkin1 第 47 行 HashMap<String, String> user= sessionManager.GetCheckInInfo();getId = user.get(sessionManager.ID); 我相信这可能是我的 session 管理器中的问题( SessionManager 的第 130 行 - GetCheckInInfo();),但我不确定,因为它与我一直在交叉引用和工作的其他类几乎相同美好的。任何帮助,将不胜感激!
session 管理器.java

public class SessionManager {

SharedPreferences sharedPreferences;
public SharedPreferences.Editor editor;
public Context context;
int Private_Mode = 0;

private static final String PREF_NAME= "LOGIN";
private static final String LOGIN = "IS_LOGIN";
public static final String USERNAME= "USERNAME";
public static final String EMAIL= "EMAIL";
public static final String ID = "ID";
public static final String PHONE = "PHONE_NUMBER";
private static final String ADDSM= "ADDITIONAL_SOCIAL_MEDIA";
private static final String HCNAME = "HOME_COUNTRY_EMERGENCY_CONTACT_NAME";
public static final String HCPHONE = "HOME_COUNTRY_EMERGENCY_PHONE_NUMBER";
public static final String HCEMAIL= "HOME_COUNTRY_EMERGENCY_CONTACT_EMAIL";
public static final String USANAME = "USA_EMERGENCY_CONTACT_NAME";
public static final String USAPHONE= "USA_EMERGENCY_PHONE_NUMBER";
public static final String USAEMAIL= "USA_EMERGENCY_CONTACT_EMAIL";
public static final String WHATSAPP = "WHATSAPP";
public static final String ADDRESS= "ADDRESS";
public static final String START_DATE= "START_DATE";
public static final String END_DATE= "END_DATE";


public SessionManager(Context context){
this.context = context;
sharedPreferences = context.getSharedPreferences(PREF_NAME, Private_Mode);
editor = sharedPreferences.edit();
}
public void createSession(String username, String email, String id){
editor.putBoolean(LOGIN, true);
editor.putString(USERNAME, username);
editor.putString(EMAIL, email);
editor.putString(ID, id);
editor.apply();

}
public boolean isLoggin(){
return sharedPreferences.getBoolean(LOGIN, false);

}
public void checkLogin(){
if (!this.isLoggin()){
Intent i = new Intent(context, Register.class);
context.startActivity(i);
((MainActivity)context).finish();
}
}

public HashMap<String, String> getUserDetail(){
HashMap<String, String> user = new HashMap<>();
user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
user.put(EMAIL, sharedPreferences.getString(EMAIL, null));
user.put(ID, sharedPreferences.getString(ID, null));

return user;
}
public void logout(){
editor.clear();
editor.commit();
Intent i = new Intent(context, LoginActivity.class);
context.startActivity(i);
((MainActivity)context).finish();
}
public void createSession2(String username, String id, String phone_number, String additional_social_media,
String home_country_emergency_contact_name, String address,
String home_country_emergency_phone_number, String home_country_emergency_contact_email,
String usa_emergency_contact_name, String usa_emergency_phone_number,
String usa_emergency_contact_email, String whatsapp){
editor.putBoolean(LOGIN, true);
editor.putString(USERNAME, username);
editor.putString(ID, id);
editor.putString(PHONE, phone_number);
editor.putString(ADDRESS, address);
editor.putString(HCPHONE, home_country_emergency_phone_number);
editor.putString(HCNAME, home_country_emergency_contact_name);
editor.putString(HCEMAIL, home_country_emergency_contact_email);
editor.putString(USAPHONE, usa_emergency_phone_number);
editor.putString(USANAME, usa_emergency_contact_name);
editor.putString(USAEMAIL, usa_emergency_contact_email);
editor.putString(WHATSAPP, whatsapp);
editor.putString(ADDSM, additional_social_media);
editor.apply();

}
public void createSessionMCI(String username, String id, String phone_number, String email, String address){
editor.putBoolean(LOGIN, true);
editor.putString(USERNAME, username);
editor.putString(ID, id);
editor.putString(PHONE, phone_number);
editor.putString(ADDRESS, address);
editor.putString(EMAIL, email);
editor.apply();

}
public HashMap<String, String> getUserDetail2(){
HashMap<String, String> user = new HashMap<>();
user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
user.put(ID, sharedPreferences.getString(ID, null));
user.put(ADDRESS, sharedPreferences.getString(ADDRESS, null));
user.put(WHATSAPP, sharedPreferences.getString(WHATSAPP, null));
user.put(PHONE, sharedPreferences.getString(PHONE, null));
user.put(ADDSM, sharedPreferences.getString(ADDSM, null));
user.put(HCNAME, sharedPreferences.getString(HCNAME, null));
user.put(HCPHONE, sharedPreferences.getString(HCPHONE, null));
user.put(HCEMAIL, sharedPreferences.getString(HCEMAIL, null));
user.put(USANAME, sharedPreferences.getString(USANAME, null));
user.put(USAPHONE, sharedPreferences.getString(USAPHONE, null));
user.put(USAEMAIL, sharedPreferences.getString(USAEMAIL, null));
return user;
}
public HashMap<String, String> getUserDates() {
HashMap<String, String> user = new HashMap<>();
user.put(START_DATE, sharedPreferences.getString(START_DATE, null));
user.put(END_DATE, sharedPreferences.getString(END_DATE, null));
user.put(ID, sharedPreferences.getString(ID, null));

return user;
}
public HashMap<String, String> GetCheckInInfo() {
HashMap<String, String> user = new HashMap<>();
user.put(USERNAME, sharedPreferences.getString(USERNAME, null));
user.put(EMAIL, sharedPreferences.getString(EMAIL, null));
user.put(PHONE, sharedPreferences.getString(PHONE, null));
user.put(ADDRESS, sharedPreferences.getString(ADDRESS, null));
user.put(ID, sharedPreferences.getString(ID, null));

return user;
}
}
checkin1.java
 private static final String TAG = checkin1.class.getSimpleName();
EditText phone_number, address, email, username;
SessionManager sessionManager;
private static String URL_ReadCheckin= "http://192.168.0.86:80/ReadCheckIns.php";
private static String URL_EDITCheckIN = "http://192.168.0.86:80/edit_detailcheckin.php";
String getId;
Button GoToJournal, GoToMain;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_checkin1);

HashMap<String, String> user= sessionManager.GetCheckInInfo();
getId = user.get(sessionManager.ID);

phone_number = findViewById(R.id.updatePhone);
email = findViewById(R.id.updateEmail);
address = findViewById(R.id.updateAddress);
username = findViewById(R.id.updateUsername);
GoToJournal = findViewById(R.id.myjournalbtn);
GoToMain = findViewById(R.id.mainmenubtn);


GoToMain.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(checkin1.this, MainActivity.class));
SaveEditDetail();
}
});

GoToJournal.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(checkin1.this, myJournal.class));
SaveEditDetail();
}
});


}
// get info
private void getCheckInDetails(){

final ProgressDialog progressDialog = new ProgressDialog(this);
progressDialog.setMessage("Loading...");
progressDialog.show();

StringRequest stringRequest = new StringRequest(Request.Method.POST, URL_ReadCheckin,
new Response.Listener<String>() {
@Override
public void onResponse(String response) {
progressDialog.dismiss();
Log.i(TAG, response.toString());
try {
JSONObject jsonObject = new JSONObject(response);
String success = jsonObject.getString("success");
JSONArray jsonArray = jsonObject.getJSONArray("read");

if(success.equals("1")){
for (int i =0; i < jsonArray.length(); i++){
JSONObject object= jsonArray.getJSONObject(i);

String strUserName = object.getString("username").trim();
String strEmail = object.getString("email").trim();
String strAddress = object.getString("address").trim();
String strPhone = object.getString("phone_number").trim();

phone_number.setText(strPhone);
address.setText(strAddress);
username.setText(strUserName);
email.setText(strEmail);
}
}
} catch (JSONException e) {
e.printStackTrace();
progressDialog.dismiss();
Toast.makeText(checkin1.this, "Error Reading Details " + e.toString(), Toast.LENGTH_SHORT).show();
}
}
},
new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
progressDialog.dismiss();
Toast.makeText(checkin1.this, "Error Reading Details " + error.toString(), Toast.LENGTH_SHORT).show();

}
})
{
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> params = new HashMap<>();
params.put("id", getId);
return params;
}
};

RequestQueue requestQueue = Volley.newRequestQueue(this);
requestQueue.add(stringRequest);
}
@Override
protected void onResume(){
super.onResume();
getCheckInDetails();
}```

最佳答案

NullPointerException is thrown when program attempts to use an objectreference that has the null value.

sessionManager = new SessionManager(checkin1.this);
HashMap<String, String> user= sessionManager.GetCheckInInfo();
getId = user.get(sessionManager.ID);

关于java - 由于 NullPointerException : Attempt to invoke virtual method HashMap on a null object reference 无法启动 Activity ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62898202/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com