首次用户登录android后显示空值

Alw*_*sed 9 java sqlite android

我在应用程序中有3个片段,其中一个我在SQLite数据库中显示用户名.当我注册一个新用户并第一次使用它登录时,会在用户名称假设出现的textview内部显示NULL值,但是当我注销并再次使用同一用户登录时,名称就会显示出来.

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

什么想法会导致这个问题?我会根据请求添加一些代码,因为我不知道代码,片段或java文件的哪一部分可能会导致这个问题.

EDITED

我添加了一些代码来帮助解决此问题.

主屏幕内的登录功能(一旦应用启动):

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);
}
Run Code Online (Sandbox Code Playgroud)

内部片段中的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);

}
Run Code Online (Sandbox Code Playgroud)

注册功能部分:

  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();
        }
    }) {
Run Code Online (Sandbox Code Playgroud)

Alw*_*sed 6

我设法自己解决这个问题..问题不是在java文件中,而是由于某种原因发送到java的php文件..

在php函数中我用过:

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

但后来改为

$user = $stmt->get_result()->fetch_assoc(); 
Run Code Online (Sandbox Code Playgroud)

这解决了问题..