简体   繁体   English

asynctask从处理程序调用时没有获得arraylist

[英]asynctask not getting arraylist when called from a handler

I'm using a handler to start an asynctask but when doing so, my application crashes. 我正在使用一个处理程序来启动asynctask,但是当这样做时,我的应用程序崩溃了。 The reason I am stuck is because if I start the asynctask via anything else (eg. onClickListener) then I can run it as many times, over and over again, and it works perfect every single time. 我被卡住的原因是因为如果我通过其他任何东西(例如onClickListener)启动asynctask,那么我可以反复运行它多次,并且它每次都能完美运行。 As soon as I execute the asynctask from my handler, it immediately crashes the application with a NullPointerException. 一旦我从我的处理程序执行asynctask,它会立即使用NullPointerException崩溃应用程序。

My handler looks something like this 我的处理程序看起来像这样

  public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
        handler.post(new Runnable() {
          @Override
          public void run() {
            new sortNearby().execute();
          }

        });
    }
  };

Here is part of the stack trace from the application showing the exception 以下是显示异常的应用程序的堆栈跟踪的一部分

Caused by: java.lang.NullPointerException
at badams.android.app.fragments.MainMenu_NearbyFragment$sortNearby.doInBackground(MainMenu_NearbyFragment.java:100)

Line 100 of my code is the first line in the asynctask under doInBackground 我的代码的第100行是doInBackground下的asynctask中的第一行

protected String doInBackground(String... args) {
  for (int i = 0; i < global.places.size(); i++) { //this is line 100

I understand that the exception is more than likely coming from "global.places.size()" probably being null, but I am stuck on why its doing that only when called from the handler, as it works fine if I start the task from any other section of my code. 我理解异常很可能来自“global.places.size()”可能是null,但我坚持为什么只有当从处理程序调用它时才这样做,因为如果我从我的代码的任何其他部分。

EDIT 编辑

As requested by @Raghunandan, here is the entire code block from doInBackground in my asynctask, which calculates the distance between a "place" and the user: 根据@Raghunandan的要求,这里是我的asynctask中doInBackground的整个代码块,它计算“地点”和用户之间的距离:

class sortNearby extends AsyncTask<String, Place, String> {
protected String doInBackground(String... args) {
  for (int i = 0; i < global.places.size(); i++) { //THIS IS LINE 100
    Location locationA = new Location("place");
    locationA.setLatitude(global.places.get(i).getLatitude());
    locationA.setLongitude(global.places.get(i).getLongitude());
    Location locationB = new Location("user");
    locationB.setLatitude(global.applicationLocationManager.getLatitude());
    locationB.setLongitude(global.applicationLocationManager.getLongitude());
    float dist = locationA.distanceTo(locationB);
    dist = dist / 1000;
    global.places.get(i).setDistance(dist);
  }
  return null;
}

EDIT 2 编辑2

global is a class extending Application and is defined in the Activity like so: global是一个扩展Application的类,在Activity中定义如下:

global = (ApplicationGlobal) getActivity().getApplicationContext();

If the NullPointerException is at line 100 then either global or global.places is returning null . 如果NullPointerException在第100行,则globalglobal.places返回null

Did you try debugging the same? 你试过调试吗?

Debugging will help you follow what is happening. 调试将帮助您了解正在发生的事情。

Also from the handler I do not see how your doInBackground method gets called? 同样从处理程序我看不到你的doInBackground方法如何被调用?

Where is the global variable defined? 定义global变量在哪里? Where and when it gets initialized? 它何时何地初始化?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM