Ho 3 frammenti all'interno di un'app e in uno di essi visualizzo il nome degli utenti dal database SQLite. Quello che succede è quando registro un nuovo utente e faccio il login per la prima volta con esso, all'interno della textview dove appare il nome dell'utente, esso visualizza il valore NULL, ma quando effettuo il logout e riconnesso con lo stesso utente, il nome appare come dovrebbe.Visualizzazione di valori nulli dopo il primo accesso degli utenti all'interno di Android
Dopo aver registrato l'utente, tutti i dati sono stati inseriti in un database, ho verificato.
Qualche idea su cosa può causare questo problema? Vorrei aggiungere qualche codice su richiesta, come non ho idea di quale parte del codice, frammenti o file java potrebbero causare questo ..
a cura
Ho aggiunto un po 'di codice per aiutare a risolvere questo problema.
funzione di login all'interno della schermata principale (una volta lancia app):
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);
}
func onViewCreated all'interno frammento in cui il nome degli utenti dovrebbe essere diplayed: funzione
@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);
}
Registrati parte:
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();
}
}) {
sei tu sicuro che la variabile non debba essere nulla? Lo assegni e poi lo visualizzi? Probabilmente aiuterebbe comunque con qualche codice da cui partire. – 3kings
Il problema può essere in qualsiasi momento - il database non restituisce il nome utente, il livello controller/DAO che non restituisce il nome corretto al client o il codice client non viene elaborato correttamente e quindi non viene visualizzato. Quindi, idealmente dovresti mettere la porzione pertinente da tutti e 3 i livelli. – hagrawal
una volta che l'utente è registrato, i suoi dettagli vengono aggiunti all'interno dei database mysql e sqlite. Tutti i dati sono inseriti, ma non c'è nulla da mostrare. aggiungerò del codice in un secondo – AlwaysConfused