在第一个用户loginandroid后显示空值

我在应用程序内部有3个片段,其中一个显示SQLite数据库中的用户名。 会发生什么事是当我注册一个新用户并首次login它时,在用户名假设出现的textview内部,它显示NULL值,但是当我用同一个用户注销并重新login时,名称会出现。

注册用户后,所有的数据都被插入到数据库中,我已经检查过了。

任何想法可以导致这个问题? 我会在请求中添加一些代码,因为我不知道代码,片段或java文件的哪一部分可能会导致这种情况。

EDITED

我已经添加了一些代码来帮助解决这个问题。

主屏幕内的loginfunction(一旦启动应用程序):

private void checkLogin(final String email, final String password) { // Tag used to cancel the request String tag_string_req = "req_login"; pDialog.setMessage("Logging in ..."); showDialog(); StringRequest strReq = new StringRequest(Request.Method.POST, AppConfig.URL_LOGIN, new Response.Listener<String>() { @Override public void onResponse(String response) { Log.d(TAG, "Login Response: " + response.toString()); hideDialog(); try { JSONObject jObj = new JSONObject(response); boolean error = jObj.getBoolean("error"); // Check for error node in json if (!error) { // user successfully logged in // Create login session session.setLogin(true); // Now store the user in SQLite String uid = jObj.getString("uid"); JSONObject user = jObj.getJSONObject("user"); String name = user.getString("name"); String email = user.getString("email"); String created_at = user.getString("created_at"); // Inserting row in users table db.addUser(name, email, uid, created_at); // Launch main activity Intent intent = new Intent(Main.this, Logged.class); startActivity(intent); finish(); } else { error_msg.setVisibility(View.VISIBLE); String msg = jObj.getString("error_msg"); error_msg.setText(msg); } } catch (JSONException e) { // JSON error e.printStackTrace(); Toast.makeText(getApplicationContext(), "Json error: " + e.getMessage(), Toast.LENGTH_LONG).show(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, "Login Error: " + error.getMessage()); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); hideDialog(); } }) { @Override protected Map<String, String> getParams() { // Posting parameters to login url Map<String, String> params = new HashMap<String, String>(); params.put("email", email); params.put("password", password); return params; } }; // Adding request to request queue AppController.getInstance().addToRequestQueue(strReq, tag_string_req); } 

onViewCreated func里面的用户名应该被显示的片段:

 @Override public void onViewCreated(View view, Bundle savedInstanceState){ profileName = (TextView) getActivity().findViewById(R.id.profileName); // SqLite database handler db = new SQLiteHandler(getActivity().getApplicationContext()); // session manager session = new SessionManager(getActivity().getApplicationContext()); if (!session.isLoggedIn()) { logoutUser(); } // Fetching user details from SQLite HashMap<String, String> user = db.getUserDetails(); String name = user.get("name"); // Displaying the user details on the screen profileName.setText(name); } 

注册function部分:

  public void onResponse(String response) { Log.d(TAG, "Register Response: " + response.toString()); hideDialog(); try { JSONObject jObj = new JSONObject(response); boolean error = jObj.getBoolean("error"); if (!error) { // User successfully stored in MySQL // Now store the user in sqlite String uid = jObj.getString("uid"); JSONObject user = jObj.getJSONObject("user"); String name = user.getString("name"); String email = user.getString("email"); String created_at = user.getString("created_at"); // Inserting row in users table db.addUser(name, email, uid, created_at); // Launch login activity Intent intent = new Intent( Register.this, Main.class); startActivity(intent); finish(); } else { // Error occurred in registration. Get the error // message error_msg.setVisibility(View.VISIBLE); String msg = jObj.getString("error_msg"); error_msg.setText(msg); } } catch (JSONException e) { e.printStackTrace(); } } }, new Response.ErrorListener() { @Override public void onErrorResponse(VolleyError error) { Log.e(TAG, "Registration Error: " + error.getMessage()); Toast.makeText(getApplicationContext(), error.getMessage(), Toast.LENGTH_LONG).show(); hideDialog(); } }) { 

Solutions Collecting From Web of "在第一个用户loginandroid后显示空值"

我设法自己sorting这个问题..问题是不是在Java文件,但在PHP文件发送到Java出于某种原因..

在我使用的PHP函数中:

 $stmt->bind_result($id, $unique_id, $name, $email, $encrypted_password, $salt, $created_at, $updated_at); $user = $stmt->fetch(); 

但后来改成了

 $user = $stmt->get_result()->fetch_assoc(); 

而这解决了这个问题..