Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/191.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
未从TimerTask android调用AsyncTask_Android_Android Asynctask_Handler_Timertask - Fatal编程技术网

未从TimerTask android调用AsyncTask

未从TimerTask android调用AsyncTask,android,android-asynctask,handler,timertask,Android,Android Asynctask,Handler,Timertask,我正在开发一个以30秒的间隔从服务器上连续获取数据的项目,所以我使用了带计时器的处理程序并调用了Asynctask 但我的asynctask没有调用。这是我的代码 final Handler handler; handler = new Handler(); timer = new Timer(); TimerTask doAsynchronousTask = new TimerTask() {

我正在开发一个以30秒的间隔从服务器上连续获取数据的项目,所以我使用了带计时器的处理程序并调用了Asynctask

但我的asynctask没有调用。这是我的代码

      final Handler handler;
         handler = new Handler();
           timer = new Timer();
          TimerTask doAsynchronousTask = new TimerTask()
          {       
              @Override
              public void run() {
                  handler.post(new Runnable() {
                      public void run() {       
                          try
                          {

                              System.out.println("I am xxx");
                              LiveTrack myActivity = new LiveTrack();
                              AsyncTask<String, String, String> task = myActivity.new VehiclePath();
                                task.execute();




                          } 
                          catch (Exception e) 
                          {

                          }
                      }
                  });
              }
          };
          timer.schedule(doAsynchronousTask, 0, 30000);
最终处理程序;
handler=新的handler();
定时器=新定时器();
TimerTask doAsynchronousTask=新TimerTask()
{       
@凌驾
公开募捐{
handler.post(新的Runnable(){
public void run(){
尝试
{
System.out.println(“我是xxx”);
LiveTrack myActivity=新建LiveTrack();
AsyncTask task=myActivity.new VehiclePath();
task.execute();
} 
捕获(例外e)
{
}
}
});
}
};
timer.schedule(doAsynchronousTask,0,30000);

有人能告诉我为什么我要面对这个问题吗?

您不能在TimerTask中创建AsyncTask,因为它在派生线程上运行,即。 不是UI线程)

只能在UI线程上创建和执行asyntask()-ed。

相反,使用执行器进行后台处理,并在需要更新UI时调用

 ExecutorService executorPool =  Executors.newSingleThreadExecutor();

 executorPool.execute(new Runnable() {
    public void run() {
        // do background processing here <------------------

        myActivity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                   // update ui here <--------------------
              }
        })
    }
});
ExecutorService executorPool=Executors.newSingleThreadExecutor();
executorPool.execute(新的Runnable(){
公开募捐{

//在此处执行后台处理您无法从TimerTask中创建异步任务,因为它在派生线程上运行,即。 不是UI线程)

只能在UI线程上创建和执行asyntask()-ed。

相反,使用执行器进行后台处理,并在需要更新UI时调用

 ExecutorService executorPool =  Executors.newSingleThreadExecutor();

 executorPool.execute(new Runnable() {
    public void run() {
        // do background processing here <------------------

        myActivity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                   // update ui here <--------------------
              }
        })
    }
});
ExecutorService executorPool=Executors.newSingleThreadExecutor();
executorPool.execute(新的Runnable(){
公开募捐{

//在此处执行后台处理您无法从TimerTask中创建异步任务,因为它在派生线程上运行,即。 不是UI线程)

只能在UI线程上创建和执行asyntask()-ed。

相反,使用执行器进行后台处理,并在需要更新UI时调用

 ExecutorService executorPool =  Executors.newSingleThreadExecutor();

 executorPool.execute(new Runnable() {
    public void run() {
        // do background processing here <------------------

        myActivity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                   // update ui here <--------------------
              }
        })
    }
});
ExecutorService executorPool=Executors.newSingleThreadExecutor();
executorPool.execute(新的Runnable(){
公开募捐{

//在此处执行后台处理您无法从TimerTask中创建异步任务,因为它在派生线程上运行,即。 不是UI线程)

只能在UI线程上创建和执行asyntask()-ed。

相反,使用执行器进行后台处理,并在需要更新UI时调用

 ExecutorService executorPool =  Executors.newSingleThreadExecutor();

 executorPool.execute(new Runnable() {
    public void run() {
        // do background processing here <------------------

        myActivity.runOnUiThread(new Runnable() {
              @Override
              public void run() {
                   // update ui here <--------------------
              }
        })
    }
});
ExecutorService executorPool=Executors.newSingleThreadExecutor();
executorPool.execute(新的Runnable(){
公开募捐{


//在此处执行后台处理无法访问LiveTrack类型的封闭实例。必须使用LiveTrack类型的封闭实例(例如x.new A(),其中x是LiveTrack的实例)限定分配.为此,我得到了我的类的实例What's LiveTrack?它是一个
活动
,还是只是名称不好?如果它是一个适当的
活动
,你知道你不能只是实例化它,对吗?是的,你是对的…但我在尝试调用Asyntask时遇到了那个错误什么错误?你没有发布错误,你只是说了你的
异步数据sk
没有被调用。我猜它没有被调用,因为应用程序无法通过
活动的实例化。将
异步任务
设置为静态,或者更好的是,将其从
活动
类中完全拉出。不能访问LiveTrack类型的封闭实例。必须使用封闭项限定分配g LiveTrack类型的实例(例如x.new A(),其中x是LiveTrack的实例).为此,我得到了我的类的实例What's LiveTrack?它是一个
活动
,还是只是名称不好?如果它是一个适当的
活动
,你知道你不能只是实例化它,对吗?是的,你是对的…但我在尝试调用Asyntask时遇到了那个错误什么错误?你没有发布错误,你只是说了你的
异步数据sk
没有被调用。我猜它没有被调用,因为应用程序无法通过
活动的实例化。将
异步任务
设置为静态,或者更好的是,将其从
活动
类中完全拉出。不能访问LiveTrack类型的封闭实例。必须使用封闭项限定分配g LiveTrack类型的实例(例如x.new A(),其中x是LiveTrack的实例).为此,我得到了我的类的实例What's LiveTrack?它是一个
活动
,还是只是名称不好?如果它是一个适当的
活动
,你知道你不能只是实例化它,对吗?是的,你是对的…但我在尝试调用Asyntask时遇到了那个错误什么错误?你没有发布错误,你只是说了你的
异步数据sk
没有被调用。我猜它没有被调用,因为应用程序无法通过
活动的实例化。将
异步任务
设置为静态,或者更好的是,将其从
活动
类中完全拉出。不能访问LiveTrack类型的封闭实例。必须使用封闭项限定分配g LiveTrack类型的实例(例如x.new A(),其中x是LiveTrack的实例).为此,我得到了我的类的实例What's LiveTrack?它是一个
活动
,还是只是命名不当?如果它是一个适当的
活动
,你知道你不能只是实例化它,对吗?是的,你是对的…但我在尝试调用