Android 从计时器或处理程序访问视图

Android 从计时器或处理程序访问视图,android,timer,delay,repeat,Android,Timer,Delay,Repeat,应用程序崩溃,我无法在Timer类中使用相同的视图 我需要应用程序刷新一个值并显示它 Timer t = new Timer(); //Set the schedule function and rate t.scheduleAtFixedRate(new TimerTask() { @Override public void run() {

应用程序崩溃,我无法在Timer类中使用相同的视图

我需要应用程序刷新一个值并显示它

Timer t = new Timer();
//Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() {

                              @Override
                              public void run() {
                                  //Called each time when 1000 milliseconds (1 second) (the period parameter)
                                  find(v);
                              }

                          },
//Set how long before to start calling the TimerTask (in milliseconds)
            0,
//Set the amount of time between each execution (in milliseconds)
            10000);

public void find(View view) {
    if (btAdapter.isDiscovering()) {
        //the button is pressed when it discovers, so cancel the discovery
        btAdapter.cancelDiscovery();
    } else {
        BTArrayAdapter.clear();
        btAdapter.startDiscovery();

        registerReceiver(bReceiver, new IntentFilter(BluetoothDevice.ACTION_FOUND));
    }
}

通常在这种情况下,您需要使用Activity.runOnUiThread

 Timer t = new Timer();
//Set the schedule function and rate
    t.scheduleAtFixedRate(new TimerTask() {

                          @Override
                          public void run() {
                             //Called each time when 1000 milliseconds   (1 second) (the period parameter)
                              runOnUiThread(new Runnable() {

                                  public void run() {find(v);}
                             });
                          }

                      },
//Set how long before to start calling the TimerTask (in milliseconds)
            0,
//Set the amount of time between each execution (in milliseconds)
            10000);

既然你没有使用视图,为什么不试试findnull而不是findv?显然仍然会崩溃-android.View.ViewRootImpl$CalledFromErrorThreadException:只有创建视图层次结构的原始线程才能接触其视图。工作:D,我还必须将视图v声明为全局变量,并使用我们的代码,你能帮我把这个代码添加到服务中吗?我需要在应用程序关闭时运行此功能不幸的是,我不知道这一点。实际上,我认为您可能需要为此发出警报: