J'ai un runnable sur un intervalle à l'aide d'un gestionnaire, qui contrôle mes demandes de volley:Android Arrêt Runnable onBackPressed()
final Handler handler = new Handler();
Runnable runnable = new Runnable() {
@Override
public void run() {
//Volley request...
AppController.getInstance().addToRequestQueue(volleyreq);
handler.postDelayed(this, 5000);
}
};
handler.postDelayed(runnable, 5000);
Je besoin de mes demandes d'arrêter quand mes utilisateurs appuyez sur le bouton retour et validez la boîte de dialogue . Cela arrête mes mises à jour de localisation, "vérifie" l'utilisateur et passe au CheckinActivity
. Puis-je d'une certaine manière insérer la méthode removeCallbacks()
à ce stade ou où exactement irait-il?
onBackPressed()
:
//Checking Out With Back Button
public void onBackPressed() {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(this);
alertDialogBuilder.setTitle("Checking Out!");
alertDialogBuilder.setMessage("Are you sure you want to check out?");
alertDialogBuilder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
//Inserting here crashes app after pressing back button
handler.removeCallbacks(runnable);
//Disconnects from location updates...
mGoogleApiClient.disconnect();
//Fires a volley POST request...
checkout();
Intent intent=new Intent(OrderListActivity.this,CheckinActivity.class);
startActivity(intent);
OrderListActivity.this.finish();
}
});
alertDialogBuilder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
alertDialog.show();
}
J'ai essayé le tir à partir de divers endroits, mais il se bloque toujours mon application et me donner une exception de pointeur nul.
journal des blocages:
08-20 19:41:08.540 2815-2815/com.myapp.workingprojectlocal E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.myapp.workingprojectlocal, PID: 2815
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.os.Handler.removeCallbacks(java.lang.Runnable)' on a null object reference
at com.myapp.workingprojectlocal.OrderListActivity$12.onClick(OrderListActivity.java:394)
at android.support.v7.app.AlertController$ButtonHandler.handleMessage(AlertController.java:161)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Veuillez fournir le journal des pannes et ping-moi. – arts777
arrêter les mises à jour de localisation serait une bonne idée dans onPause méthode –
Je l'ai changé pour onPause et ça marche! Bon à savoir cela fonctionne dans divers endroits :) – glokul