从AsyncTask更新TextView

时间:2017-01-20 10:25:28

标签: java android android-asynctask

我正在尝试将this example应用于我的项目,但我收到以下错误:

Error:(26, 33) error: cannot find symbol method findViewById(int)

这就是我的代码的样子:

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.os.AsyncTask;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }
}


class Connect extends AsyncTask<Void, Void, Integer> {

    TextView Text1 = (TextView) findViewById(R.id.textView1);

    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }
}
  • 我在这里缺少什么?
  • 如何通过AsyncTask引用TextView?

4 个答案:

答案 0 :(得分:0)

这是不正确的:

class Connect extends AsyncTask<Void, Void, Integer> {

    TextView Text1 = (TextView) findViewById(R.id.textView1);

findViewByIdActivity类的方法。不是AsyncTask

修复它,使用它:

Connect myConnect = new Connect(display);

并创建构造函数:

class Connect extends AsyncTask<Void, Void, Integer> {
   TextView Text1;
public Connect(TextView tv) {
Text1 = tv;
}

你又得到了另一个错误:

 @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }

将其改为:

 @Override
    protected void onProgressUpdate(Integer... values) {
      super.onProgressUpdate(values);
      Text1.setText("Text Changed");
    }

<强>总结 //一个文件     公共类MainActivity扩展了AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        TextView display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
       Connect myConnect = new Connect(display);
        myConnect.execute();
    }
}

//可能是另一个文件。但你可以把下面的类作为嵌套 - 只需将前一个paranthesis移动到代码的末尾         class Connect extends AsyncTask {                TextView Text1;             public Connect(TextView tv){             Text1 = tv;             }             @覆盖             protected void onPreExecute(){             }

        @Override
        protected Integer doInBackground(Void... params) {

        /*publish your progress here
          publishProgress(...);
        */
            return 0;
        }

        @Override
        protected void onProgressUpdate(Void... values) {
            super.onProgressUpdate(values);
            Text1.setText("Text Changed");
        }
    }

答案 1 :(得分:0)

您的文本视图与您的MainActivity相关,因此仅在活动类中创建全局引用,并将其用于子类。

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;
import android.os.AsyncTask;

import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

TextView Text1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Text1 = (TextView) findViewById(R.id.textView1);
        Text1.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }



class Connect extends AsyncTask<Void, Void, Integer> {

    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {
        super.onPostExecute(0);
        Text1.setText("Text Changed");
    }
}


}

答案 2 :(得分:-1)

在AsyncTask中无法访问findViewById(),该方法属于activity,因此您无法使用Asynctask中的id初始化视图。在onCreate中的活动中初始化您的视图,并在异步任务中使用该实例

public class MainActivity extends AppCompatActivity {
TextView display;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        display = (TextView) findViewById(R.id.textView1);
        display.setText("Hello Android!");
        Connect myConnect = new Connect();
        myConnect.execute();
    }


class Connect extends AsyncTask<Void, Void, Integer> {


    @Override
    protected void onPreExecute(){
    }

    @Override
    protected Integer doInBackground(Void... params) {
        return 0;
    }

    @Override
    protected void onProgressUpdate(Void... values) {


    }
    protected void onPostExecute(Long result) {
    display.setText("Text Changed");
 }
}}

答案 3 :(得分:-1)

onCreate()中使用您的观点。将在主应用程序线程(a.k.a.,UI线程)上调用onProgressUpdate()。如CommansWare

所述

全局

private TextView Text1;

用作:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    TextView display = (TextView) findViewById(R.id.textView1); 
    Text1 = (TextView) findViewById(R.id.textView1); //here
       .........

按原样设置

@Override
protected void onProgressUpdate(Void... values) {
    super.onPostExecute(0);
    Text1.setText("Text Changed");
}