Java 如何在AsyncTask中从回调函数访问父类视图?

Java 如何在AsyncTask中从回调函数访问父类视图?,java,android,callback,android-asynctask,parent,Java,Android,Callback,Android Asynctask,Parent,这是我的示例代码 public class test extends Activity { private TextView tv; public class Blap extends AsyncTask<String,String,Boolean>{ private test parent; @Override protected Boolean doInBackground(String... options) { this.au

这是我的示例代码

public class test extends Activity {
  private TextView tv;

  public class Blap extends AsyncTask<String,String,Boolean>{
    private test parent;
    @Override
    protected Boolean doInBackground(String... options) {
        this.auth(options[0],options[1]);
        parent.aresponse(res);
        return true;
    }
    public Blap(test a){
        this.parent = a;
    }
    public auth(String login, String password){
        //process auth
    }
}

@Override
public void onCreate(Bundle savedInstanceState) {
    //work...
}

public void buttonclick(View v){
    tv = new TextView(this);
    new Blap(this).execute(editText1.getText().toString(),editText2.getText().toString());
    setContentView(tv);
}
public void aresponse(String rs){
    tv.setText(rs);
}
}
公共类测试扩展活动{
私家图文电视;
公共类Blap扩展异步任务{
私人测试父母;
@凌驾
受保护的布尔doInBackground(字符串…选项){
this.auth(选项[0],选项[1]);
父母的反应(res);
返回true;
}
公共Blap(测试a){
this.parent=a;
}
公共身份验证(字符串登录、字符串密码){
//进程身份验证
}
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
//工作。。。
}
公共无效按钮单击(视图v){
tv=新文本视图(此);
新建Blap(this.execute)(editText1.getText().toString(),editText2.getText().toString());
设置内容视图(电视);
}
公共无效响应(字符串rs){
tv.setText(rs);
}
}
但是当调用aresponse时,我遇到了一个例外:只有创建视图层次结构的原始线程才能接触其视图。
如何正确等待异步操作完成并继续处理所需的操作?

只需更改您的方法
aresponse
,如下所示:

public void aresponse(String rs){
    runOnUiThread(new Runnable() {
        @Override
        public void run() {
            tv.setText(rs);             
        }
    });
}
这样,无论何时何地调用此方法,它都会在UI线程上更新您的TextView


此外,您还可以考虑从AcyCtaskOnPosiExcel方法调用这个方法,它将不会出现任何问题,但它的选择如下:)

<代码> AycCastase<代码>,有一些方法在UI线程上自动调用。覆盖

onPostExecute()
并从那里调用
aresponse()
。更多信息如下:

AsyncTask
为您提供了两种更新UI的方式:

如果要与在
doInBackground()
中执行的任务同时更新UI(例如,要更新
ProgressBar
),必须在
doInBackground()
方法中调用
publishProgress()
。然后您必须在
onProgressUpdate()
方法中更新UI

/** This method runs on another thread than the UI thread */
@Override
protected String doInBackground(String... _params) {
    for (int progressValue = 0; progressValue  < 100; progressValue++) {
        publishProgress(progressValue);
    }
}

/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
    // TODO Update your ProgressBar here
}

/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */
@Override
protected void onPostExecute(Boolean _result) {
   // TODO Update the UI thread with the final result
}
如果要在任务完成时更新UI,必须使用
onPostExecute()
方法

/** This method runs on another thread than the UI thread */
@Override
protected String doInBackground(String... _params) {
    for (int progressValue = 0; progressValue  < 100; progressValue++) {
        publishProgress(progressValue);
    }
}

/** This method runs on the UI thread */
@Override
protected void onProgressUpdate(Integer... progressValue) {
    // TODO Update your ProgressBar here
}

/**
 * Called after doInBackground() method
 * This method runs on the UI thread
 */
@Override
protected void onPostExecute(Boolean _result) {
   // TODO Update the UI thread with the final result
}

谢谢但在这个问题上,我更喜欢使用onPostExecute,正如我所知,它在UI线程中运行。这是您的选择。我的目的是通知您,您不能在非ui线程上更新视图,仅此而已:)