简体   繁体   English

在asynctask中更新UI线程

[英]updating UI thread while in asynctask

as the title states, im trying to update something in my UI thread while running an asynctask.. i've read quite a bit on asynctask and it seems i should be able to change a variable from the onPostExecute() method. 如标题所述,我试图在运行asynctask时更新我的​​UI线程中的某些内容。.我已经阅读了很多有关asynctask的内容,看来我应该能够从onPostExecute()方法中更改一个变量。 obviously this is not the case. 显然不是这样。

Here is my sample code: 这是我的示例代码:

TextView tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated); 
Login login = new Login();
login.execute(userName, password);

and here the the login class 这是登录类

public class Login extends AsyncTask<String, void, String>{
    public String doInBackground(String... params){
       logMeIn(params[0], params[1]);
    }
    public void onPostExecute(String update){
       tv.setText(result); //this is not working!!
    }

whats actually happening is tv is underlined red and eclipse says i need to create a local variable.. but i thought the onPostExecute is ran from the UI thread? 实际发生的事情是电视用红色下划线标出,并且日食说我需要创建一个局部变量..但是我认为onPostExecute是从UI线程运行的? confused :? 困惑:?
im trying to do what i found at this website. 我试图做我在这个网站上找到的东西。 I'm not entirly sure what i'm doing and i would love a point in the right direction! 我不确定自己在做什么,我很乐意朝正确的方向迈进! thanks in advance. 提前致谢。

This line here 这条线在这里

TextView tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated);

is obviously declared either 显然被宣告

  1. Outside of a method -which would result in tv being null or 在方法之外-这将导致tvnull
  2. In a method that isn't part of the AsyncTask - which would mean the task doesn't have access to it 在不属于AsyncTask -这意味着该任务无权访问

You should define it as a member variable (outside of a method) 您应该将其定义为成员变量(方法之外)

TextView tv;

then initialize it inside of a method 然后在方法内部对其进行初始化

tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated);

This will give your task access to it as well as the rest of your class. 这将使您以及其他班级的人都可以访问任务。

If your AsyncTask is a separate file than your Activity then you will want to see this answer on using an interface and create a callback to update the TextView in your Activity . 如果AsyncTask是不同于Activity的单独文件,则您将希望在使用interface 看到此答案 ,并创建一个回调以更新ActivityTextView

To bring your TextView instance into scope, pass your tv variable into your Login instance constructor and add the constructor and variable in as below. 要将您的TextView实例带入范围,请将tv变量传递到Login实例构造函数中,然后按如下所示添加构造函数和变量。

public class Login extends AsyncTask<String, void, String>{

    private TextView tv;

    public Login(TextView tv){
       this.tv = tv;
    }
    public String doInBackground(String... params){
       logMeIn(params[0], params[1]);
    }
    public void onPostExecute(String update){
       tv.setText(result); //this should work
    }
}

You have to pass the variable to this instance, otherwise it is out of scope. 您必须将变量传递给此实例,否则它将超出范围。 You won't be able to access it unless you declare and instantiate a new variable within the Login class. 除非您在Login类中声明并实例化一个新变量,否则您将无法访问它。

Place TextView tv; 放置TextView tv; into the scope of the class and outside of the function. 进入类的范围和功能之外。 Leave tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated); 离开tv = (TextView) findViewById(R.id.thingsThatNeedToBeUpdated); where it is. 在哪儿。 You should then be able to access tv from onPostExecute() . 然后,您应该能够从onPostExecute()访问tv

This is because the TextView variable tv is defined in another class that does not have Login as an inner class. 这是因为TextView变量tv是在另一个没有Login作为内部类的类中定义的。 If Login is an inner class of the class containing the variable tv, it should be able to see the variable tv. 如果Login是包含变量tv的类的内部类,则应该能够看到变量tv。

An easy way to solve this would be to make your AsyncTask an inner class of your main class. 解决此问题的简单方法是使AsyncTask成为主类的内部类。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM