Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/logging/2.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 在异步任务中引用textview_Android_Android Asynctask - Fatal编程技术网

Android 在异步任务中引用textview

Android 在异步任务中引用textview,android,android-asynctask,Android,Android Asynctask,我正在使用一个asynctask来完成一些xml工作,我想在完成后更新textview,但我遇到了以下问题,即我无法使用findViewById引用textview public class HTTPRequestTask extends AsyncTask<String , Void, String > { @Override protected String doInBackground(String... args) { q = (TextView)findVi

我正在使用一个asynctask来完成一些xml工作,我想在完成后更新textview,但我遇到了以下问题,即我无法使用findViewById引用textview

public class HTTPRequestTask extends AsyncTask<String , Void, String > {

  @Override
protected String doInBackground(String... args) {

    q = (TextView)findViewById(R.id.xmlData); //cannot resolve findViewbyId

在postExecute方法中设置文本最直接的方法是什么???

您应该参考嵌入类
HTTPRequestTask
活动类


例如:
MyActivity.this.findviewbyd(R.id.xmlData)

您必须将
活动
变量或
文本视图
变量传递到
HTTPRequestTask

我是说,创建构造函数

    public class HTTPRequestTask extends AsyncTask<String , Void, String > {

    private Activity act;
private TextView q;
        public HTTPRequestTask (Activity act) {
        this.act = act;
 q = (TextView)act.findViewById(R.id.xmlData);
        }
        @Override
        protected String doInBackground(String... args) {
        //some code with your q
公共类HTTPRequestTask扩展了AsyncTask{
私人活动法;
私有文本视图q;
公共HTTPRequestTask(活动法){
this.act=act;
q=(TextView)act.findviewbyd(R.id.xmlData);
}
@凌驾
受保护的字符串doInBackground(字符串…args){
//一些代码与你的q
要启动任务,请使用以下命令:


新建HTTPRequestTask(this).execute();

看起来,这个类在另一个java文件中。你如何确认?如果这个类是一个子类,IDE可以帮助解决这个问题。我是java新手,所以我不知道你的意思。我把q=MyActivity.this.findViewById(R.id.xmlData);在类标题下,但给出了许多错误。该如何实现?该类位于另一个java文件中。如果在单独的文件中,它的行为是否会有所不同?在这种情况下,您的代码将导致内存泄漏使用
WeakReference
。使用
WeakReference
,这是一个潜在的内存泄漏。@Enzokie,我从未遇到过这个问题使用这样的实现。当类只能接受3个参数时,如何将这些特定变量传递给类?现在可以了,但我仍然不确定它在做什么。为什么要在类中创建公共方法,“this”做什么?@MXG123,我们创建了一个构造函数,将对象作为参数传递。
    public class HTTPRequestTask extends AsyncTask<String , Void, String > {

    private Activity act;
private TextView q;
        public HTTPRequestTask (Activity act) {
        this.act = act;
 q = (TextView)act.findViewById(R.id.xmlData);
        }
        @Override
        protected String doInBackground(String... args) {
        //some code with your q