Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/212.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
Android 在主线程中等待异步任务_Android_Android Asynctask - Fatal编程技术网

Android 在主线程中等待异步任务

Android 在主线程中等待异步任务,android,android-asynctask,Android,Android Asynctask,在我的应用程序中,我需要执行网络操作,所以我创建了一个AsyncTask类来在后台执行它,但是我需要用我得到的信息填充一些文本视图 因此,我的问题是,我可以在主线程中做什么来等待AsyncTask类完成doInBackground操作 这是我的AsyncTask类: public class ObtenerDatos extends AsyncTask<Void, Void, Void> { private PuertosSaxParser saxparser; p

在我的应用程序中,我需要执行网络操作,所以我创建了一个
AsyncTask
类来在后台执行它,但是我需要用我得到的信息填充一些文本视图

因此,我的问题是,我可以在主线程中做什么来等待
AsyncTask
类完成
doInBackground
操作

这是我的AsyncTask类:

public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;

    /** Return the Puertos info to the main class */
    public ArrayList<clsPuertos> getPuertos() {
        return Puertos;
    }

    /** Get the XML info and do the parse */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            saxparser = new PuertosSaxParser("http://www.androide.comyr.com/valores.xml");
            Puertos = saxparser.parseXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
try {
    int_datos.execute();
    //HERE I NEED TO WAIT FOR THE AsyncTask TO COMPLETE THE doInBackground OPERATIONS
    Puertos = int_datos.getPuertos();
    //THEN I FILL THE TEXTVIEWs WITH THE INFO OF Puertos
} catch (Exception e) {
    e.printStackTrace();
}
ObtenerDatos int_datos = new ObtenerDatos(this);
int_datos.execute();
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;
    private Context contexto;
    final TextView textSanferPleamarHora = new TextView(contexto);

    public ObtenerDatos(Context contexto){
        this.contexto = contexto;
    }
谢谢你的帮助

编辑:

public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;

    /** Return the Puertos info to the main class */
    public ArrayList<clsPuertos> getPuertos() {
        return Puertos;
    }

    /** Get the XML info and do the parse */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            saxparser = new PuertosSaxParser("http://www.androide.comyr.com/valores.xml");
            Puertos = saxparser.parseXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
try {
    int_datos.execute();
    //HERE I NEED TO WAIT FOR THE AsyncTask TO COMPLETE THE doInBackground OPERATIONS
    Puertos = int_datos.getPuertos();
    //THEN I FILL THE TEXTVIEWs WITH THE INFO OF Puertos
} catch (Exception e) {
    e.printStackTrace();
}
ObtenerDatos int_datos = new ObtenerDatos(this);
int_datos.execute();
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;
    private Context contexto;
    final TextView textSanferPleamarHora = new TextView(contexto);

    public ObtenerDatos(Context contexto){
        this.contexto = contexto;
    }
main类:

public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;

    /** Return the Puertos info to the main class */
    public ArrayList<clsPuertos> getPuertos() {
        return Puertos;
    }

    /** Get the XML info and do the parse */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            saxparser = new PuertosSaxParser("http://www.androide.comyr.com/valores.xml");
            Puertos = saxparser.parseXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
try {
    int_datos.execute();
    //HERE I NEED TO WAIT FOR THE AsyncTask TO COMPLETE THE doInBackground OPERATIONS
    Puertos = int_datos.getPuertos();
    //THEN I FILL THE TEXTVIEWs WITH THE INFO OF Puertos
} catch (Exception e) {
    e.printStackTrace();
}
ObtenerDatos int_datos = new ObtenerDatos(this);
int_datos.execute();
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;
    private Context contexto;
    final TextView textSanferPleamarHora = new TextView(contexto);

    public ObtenerDatos(Context contexto){
        this.contexto = contexto;
    }
ObtenerDatos(异步任务类):

public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;

    /** Return the Puertos info to the main class */
    public ArrayList<clsPuertos> getPuertos() {
        return Puertos;
    }

    /** Get the XML info and do the parse */
    @Override
    protected Void doInBackground(Void... params) {
        try {
            saxparser = new PuertosSaxParser("http://www.androide.comyr.com/valores.xml");
            Puertos = saxparser.parseXML();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

}
try {
    int_datos.execute();
    //HERE I NEED TO WAIT FOR THE AsyncTask TO COMPLETE THE doInBackground OPERATIONS
    Puertos = int_datos.getPuertos();
    //THEN I FILL THE TEXTVIEWs WITH THE INFO OF Puertos
} catch (Exception e) {
    e.printStackTrace();
}
ObtenerDatos int_datos = new ObtenerDatos(this);
int_datos.execute();
public class ObtenerDatos extends AsyncTask<Void, Void, Void> {

    private PuertosSaxParser saxparser;
    private ArrayList<clsPuertos> Puertos;
    private Context contexto;
    final TextView textSanferPleamarHora = new TextView(contexto);

    public ObtenerDatos(Context contexto){
        this.contexto = contexto;
    }
公共类ObtenerDatos扩展异步任务{
私有puertosaxparser-saxparser;
私人ArrayList Puertos;
私人语境;
最终文本视图textSanferPleamarHora=新文本视图(contexto);
公共获取(上下文){
this.contexto=contexto;
}
我试图将上下文从Main传递到
AsyncTask
类,以便在那里设置
textview
,但我遇到了一些错误

是这样吗

所以我的问题是,在主线程中,我可以做什么来等待AsyncTask类完成“doInBackground”操作

不要这样做。使用类似于
AsyncTask
的东西的完整和完整的要点是不阻止主应用程序线程

请在
AsyncTask
onPostExecute()
中“用我得到的信息填充一些文本视图”

所以我的问题是,在主线程中,我可以做什么来等待AsyncTask类完成“doInBackground”操作

不要这样做。使用类似于
AsyncTask
的东西的完整和完整的要点是不阻止主应用程序线程


请在
onPostExecute()中“用我得到的信息填写一些textview”
异步任务的

任何你想设置为textView的数据你只需在postExecute方法中执行就可以了你可以从doInbackground共享你的数据只需将最后一个空格作为字符串并传递字符串

任何你想设置为textView的数据你只需在postExecute方法中执行就可以了doInbackground中的ur数据只需将最后一个空格作为字符串,并只需在onPostExecute()中传递字符串

设置文本或更新ui,或使用runonuithread un doinbackgroundd()进行更新要更新UI,您也可以像我在这里所做的那样使用回调:。基本点是您不必等待。您允许UI线程继续,以便在服务器返回数据时系统保持响应并拾取执行。在onPostExecute()中设置文本或更新UI,或使用runonuithread un doinbackgroundd()进行设置要更新UI,您也可以像我在这里所做的那样使用回调:。基本点是您不必等待。您允许UI线程继续,以便系统保持响应,并在服务器返回数据时开始执行。谢谢,我尝试按照您所说的做,但我需要获取“上下文”从main类到AsyncTask类来设置TextView,但我有一个问题,我在将上下文从main提供给另一个类时出错。我在我的问题中添加了一个新示例,对吗?谢谢again@Fox:“是这样吗?”--可能不会。通常您不会以这种方式从Java代码创建
TextView
。目前管理
AsyncTask
的最佳方法是将其保存在保留的
片段中。然后
AsyncTask
可以使用
onPostExecute()
(或
onProgressUpdate()
)中的
片段获取更新结果所需的小部件。谢谢,我试着像你说的那样做,但我需要获取“上下文”从main类到AsyncTask类来设置TextView,但我有一个问题,我在将上下文从main提供给另一个类时出错。我在我的问题中添加了一个新示例,对吗?谢谢again@Fox:“是这样吗?”--可能不会。通常您不会以这种方式从Java代码创建
TextView
。目前管理
AsyncTask
的最佳方法是将其保存在保留的
片段中。然后
AsyncTask
可以使用
onPostExecute()
(或
onProgressUpdate()
)中的
片段获取更新结果所需的小部件。