简体   繁体   English

从消息处理程序启动asynctask

[英]Starting asynctask from message handler

I have a gps class in my application. 我的应用程序中有一个gps类。 In the onLocationChanged method I am sending a handler message to my activity which is supposed to execute an asynctask. 在onLocationChanged方法中,我向我的activity发送一个处理程序消息,该消息应该执行asynctask。 My asyntask runs fine on its own if I call it anywhere from the activity, but I keep getting a crash when it gets called via the handler. 如果我在活动的任何地方调用它,我的asyntask就可以自行运行了,但是当它通过处理程序调用时我会一直崩溃。 I'm sure it has to do with how I'm starting the task from the handler. 我确定它与我如何从处理程序启动任务有关。

Here is my handler in my activity fragment which is supposed to start the async task 这是我的活动片段中的处理程序,它应该启动异步任务

  public Handler handler = new Handler() {
    @Override
    public void handleMessage(Message msg) {
      super.handleMessage(msg);
      Log.d("MainMenu_NearbyFragment", "handler recieved for on location changed");
      if (!sortingPlaces) {
        getActivity().runOnUiThread(new Runnable() {
          @Override
          public void run() {
            new sortNearby().execute();
          }
        });
      }
    }
  };

Im sending the message to the handler from my onLocationChanged like this 我从这个onLocationChanged发送消息给处理程序

  public void onLocationChanged(Location location) {
    new MainMenu_NearbyFragment().handler.sendEmptyMessage(0);
  }

EDIT: 编辑:

Heres a stack trace after running on an actual device. 在实际设备上运行后,继承了堆栈跟踪。 (The previous was running on emulator, this is a different message which I didnt realize at first). (之前是在模拟器上运行,这是一个不同的消息,我最初没有意识到)。

java.lang.NullPointerException
at  badams.android.alcology.fragments.MainMenu_NearbyFragment$1.handleMessage(MainMenu_NearbyFragment.java:43)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)

Line 43 is: 第43行是:

getActivity().runOnUiThread(new Runnable() {

The reason I am doing this is because I need to be able to detect locationChanged from activities not implementing the LocationListener. 我这样做的原因是因为我需要能够从未实现LocationListener的活动中检测locationChanged。 So the idea is that in onLocationChanged I am sending a message to my activity informing it to update. 所以我的想法是在onLocationChanged中我向我的活动发送一条消息,通知它更新。 Perhaps this is the wrong way for me to approach this? 也许这对我来说是错误的方法吗?

I ended up getting this working by using 我最终通过使用来完成这项工作

handler.post(new Runnable()

instead of 代替

getActivity().runOnUiThread(new Runnable() 

and that seemed to start the asynctask just fine! 这似乎开始 asynctask就好了! The asynctask isnt working properly when called from the handler, but that's unrelated to this question now. 从处理程序调用时,asynctask无法正常工作,但现在与此问题无关。 I really dont understand why that works, but it did. 我真的不明白为什么会这样,但确实如此。

use 采用

MainMenu_NearbyFragment.this.getActivity().runOnUiThread(new Runnable() {

instead of 代替

getActivity().runOnUiThread(new Runnable() {

It should work.. 它应该工作..

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

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