Good I have the following code in android where it shows me how many questions have been answered well and how many have not:
private void checkResults() {
int correctas = 0;
int incorrectas = 0;
int nocontestadas = 0;
for (int i = 0; i < all_questions.length; i++) {
if (answer_is_correct[i]) correctas++;
else if (answer[i] == -1) nocontestadas++;
else incorrectas++;
}
String message =
String.format("Correctas: %d\nIncorrectas: %d\nNo contestadas: %d\n",
correctas, incorrectas, nocontestadas);
//--cuadro de dialogo
//construimos el constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//que va a tener
builder.setTitle(R.string.results);
builder.setMessage(message);
//botones
builder.setCancelable(false);//desabilita el bton atras
builder.setPositiveButton(R.string.finish, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent b=new Intent(getApplicationContext(),MenuActivity.class);
startActivity(b);
finish();
}
});
//volver a empezar
builder.setNegativeButton(R.string.start_over, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startOver();
}
});
//crea el cuadro
builder.create().show();
}
what I want to do is that the result that was obtained is saved to firebase, eh tried with the following code but it does not come out (the application stops)
String nombre = String.valueOf(correctas);
mDataBase.child(mAuth.getCurrentUser().getUid()).child("progreso").child("suma").child("pregunta1").setValue(nombre);
it would be like this my code
private void checkResults() {
int correctas = 0;
int incorrectas = 0;
int nocontestadas = 0;
for (int i = 0; i < all_questions.length; i++) {
if (answer_is_correct[i]) correctas++;
else if (answer[i] == -1) nocontestadas++;
else incorrectas++;
}
String message =
String.format("Correctas: %d\nIncorrectas: %d\nNo contestadas: %d\n",
correctas, incorrectas, nocontestadas);
//--cuadro de dialogo
//construimos el constructor
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//que va a tener
builder.setTitle(R.string.results);
builder.setMessage(message);
//botones
builder.setCancelable(false);//desabilita el bton atras
builder.setPositiveButton(R.string.finish, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent b=new Intent(getApplicationContext(),MenuActivity.class);
startActivity(b);
finish();
}
});
//volver a empezar
builder.setNegativeButton(R.string.start_over, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
startOver();
}
});
//crea el cuadro
builder.create().show();
// aqui implemento el codigo firebase
String nombre = String.valueOf(correctas);
mDataBase.child(mAuth.getCurrentUser().getUid()).child("progreso").child("suma").child("pregunta1").setValue(nombre);
}
I get the following error:
FATAL EXCEPTION: main
Process: com.juegosludicos.juegosludicosmejoradodos, PID: 26366
java.lang.NullPointerException: Attempt to invoke virtual method 'com.google.firebase.auth.FirebaseUser com.google.firebase.auth.FirebaseAuth.getCurrentUser()' on a null object reference
at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity.checkResults(PreguntaFacilActivity.java:217)
at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity.access$400(PreguntaFacilActivity.java:23)
at com.juegosludicos.juegosludicosmejoradodos.PreguntaFacilActivity$1.onClick(PreguntaFacilActivity.java:137)
at android.view.View.performClick(View.java:5246)
at android.widget.TextView.performClick(TextView.java:10620)
at android.view.View$PerformClick.run(View.java:21256)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:6934)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1404)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1199)
the lines you point to is the line where you implement the firebase code