我正在尝试开始 Activity 检入1。我使用排球,并从mySQL db请求信息以在四个编辑文本字段中显示。
我使用的是我在应用程序的其他 Activity (sessionmanager)中使用的相同策略,并且我保持交叉引用并想知道为什么在第47行出现错误的原因,我的代码是相同的:
checkin1的第47行 HashMap<String, String> user= sessionManager.GetCheckInInfo();getId = user.get(sessionManager.ID); 我相信这可能是我的 session 管理器中的一个问题(SessionManager的第130行-GetCheckInInfo();),但是我不确定,因为它与我一直在交叉引用中的其他类完全相同,并且可以正常工作。任何帮助,将不胜感激!
Sessionmanager.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();
 }```

最佳答案

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

关于java - 由于NullPointerException而无法启动 Activity :尝试在空对象引用上调用虚拟方法HashMap,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/62898202/

10-09 03:27