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

时间:2015-10-05 16:10:43

标签: android timer delay repeat

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

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

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

1 个答案:

答案 0 :(得分:0)

通常在这种情况下,您需要使用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);