Android停止运行onBackPressed()

时间:2017-08-21 00:23:21

标签: android android-volley handler runnable

我使用处理程序在一个间隔上有一个runnable,它控制我的截击请求:

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);

当用户按下后退按钮并确认对话框时,我需要停止请求。这会停止我的位置更新,"检查"用户输出,并切换到CheckinActivity。我可以以某种方式在此时插入removeCallbacks()方法,或者它究竟会在哪里插入?

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();

}

我尝试过从各个地方开火,但它总是崩溃我的应用并给我一个空指针异常。

崩溃日志:

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)

2 个答案:

答案 0 :(得分:0)

终于有了这个工作。 NPE原因是没有初始化处理程序类。在我修复了runnable仍然不会停止handler.removeCallbacks(runnable);似乎正在起作用的是:

handler.removeCallbacksAndMessages(null);

答案 1 :(得分:0)

这意味着您的处理程序是NULL。这样做:

if (handler !=null && runnable !=null)
   handler.removeCallbacks(runnable);

应该解决你的问题。