1

这是我的场景。A 类实现了 Runnable。当用户单击一个按钮时,将显示一个进度对话框并调用该方法searchMap()来搜索地址。对话框在 10 秒后消失。我真的误解了如何执行该run()方法。这是我令人毛骨悚然的代码。

public class AddLocationMapActivity extends MapActivity implements Runnable {
private ProgressDialog progressDialog;
private MyHandler myHandler; 
private Message msg;

@Override
public void run() {
    mapCurrentAddress();
}
public void mapLocation(View v) // click event here{
    progress();
    Thread thread = new Thread(this);
    thread.start();
}
private class MyHandler extends Handler{
@Override
public void handleMessage(Message msg) {    
    super.handleMessage(msg);
    switch(msg.what) {
        case NOT_OK_MESSAGE: // Fail
            alert(AddLocationMapActivity.this, message());
            progressDialog.dismiss();
        break;
        case OK_MESSAGE: // Success
            found(); // Point to the appropiate address
            progressDialog.dismiss();
        break;
        case EXPTION_MESSAGE: // Exception
            alert(AddLocationMapActivity.this, "Unexpected error");
            progressDialog.dismiss();
        break;
        }
    }
}
protected void mapCurrentAddress() {
    String addressString = addressText.getText().toString();
    Geocoder g = new Geocoder(this);
    List<Address> addresses;
    myHandler = new MyHandler();
    msg = myHandler.obtainMessage();
    try {
        addresses = g.getFromLocationName(addressString, 1);
        if (addresses.size() > 0) {
            //address = addresses.get(0);
            msg.what = OK_MESSAGE;
            myHandler.sendEmptyMessage(OK_MESSAGE);

        } else {
            // show the user a note that we failed to get an address
            myHandler.sendEmptyMessage(NOT_OK_MESSAGE);
        }
    } catch (IOException e) {
        // show the user a note that we failed to get an address
        //e.printStackTrace();
        myHandler.sendEmptyMessage(EXPTION_MESSAGE);
    }
}
    private void progress() {
    progressDialog = ProgressDialog.show(this, 
            "Checking", "Contacting Map Server");
    Thread progressThread = new Thread();
    progressThread.start();
}
}

当点击事件发生时,程序失败并出现异常Uncaught Handler

4

1 回答 1

0

这是我的一些线程代码,不会泄露太多......

progress = ProgressDialog.show(this, "", "Loading...", true);

new Thread(new Runnable() {
  @Override
  public void run()
  {
    // get some network content

    Chooser.this.runOnUiThread(new Runnable() {
      @Override
      public void run()
      {
        progress.dismiss();

        // update the UI
      }
    });
  }
}).start();
于 2012-04-06T15:48:08.783 回答