findViewById()中的AsyncTask [英] findViewById() in asyncTask

查看:126
本文介绍了findViewById()中的AsyncTask的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想用findViewById()方法中的AsyncTask类......我试图onPostExecute()和preExecute()。但它不工作

 类Proccess扩展的AsyncTask<弦乐,太虚,太虚> {    @覆盖
    保护无效doInBackground(字符串...为arg0){
    TextView的TXT =(的TextView)findViewById(R.id.tvResult); //错误的原因
        返回null;
    }    @覆盖
    在preExecute保护无效(){
        super.on preExecute();
        TextView的TXT =(的TextView)findViewById(R.id.tvResult); //错误的原因
    }    @覆盖
    保护无效onPostExecute(虚空结果){
        super.onPostExecute(结果);
        TextView的TXT =(的TextView)findViewById(R.id.tvResult); //错误的原因    }
}


解决方案

编辑code这样的

 类Proccess扩展的AsyncTask<弦乐,太虚,太虚> {@覆盖
保护无效doInBackground(字符串...为arg0){    TextView的TXT =(的TextView)findViewById(R.id.tvResult); // C使用错误
    返回null; //这应该是最后一条语句,否则会造成无法到达code。
}@覆盖
在preExecute保护无效(){
    super.on preExecute();
    TextView的TXT =(的TextView)findViewById(R.id.tvResult); // C使用错误
}@覆盖
保护无效onPostExecute(虚空结果){
    super.onPostExecute(结果);
    TextView的TXT =(的TextView)findViewById(R.id.tvResult); // C使用错误}
}

和您的工艺类应该是内部类您的活动。 其他明智它,因为方法findViewById(INT)是不确定的。

I want to use findViewById() method in AsyncTask class... i tried onPostExecute() and onPreExecute().but it doesnt work

class Proccess extends AsyncTask<String, Void, Void>{

    @Override
    protected Void doInBackground(String... arg0) {
    TextView txt = (TextView) findViewById(R.id.tvResult); // cause error
        return null;
    }

    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        TextView txt = (TextView) findViewById(R.id.tvResult); // cause error
    }

    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        TextView txt = (TextView) findViewById(R.id.tvResult); // cause error

    }
}

解决方案

Edit your code like this

class Proccess extends AsyncTask<String, Void, Void>{

@Override
protected Void doInBackground(String... arg0) {

    TextView txt = (TextView) findViewById(R.id.tvResult); // cuse error
    return null; //this should be the last statement otherwise cause unreachable code.
}

@Override
protected void onPreExecute() {
    super.onPreExecute();
    TextView txt = (TextView) findViewById(R.id.tvResult); // cuse error
}

@Override
protected void onPostExecute(Void result) {
    super.onPostExecute(result);
    TextView txt = (TextView) findViewById(R.id.tvResult); // cuse error

}
}

And your Process class should be innerclass of Your Activity. other wise it cause method findViewById(int) is undefined.

这篇关于findViewById()中的AsyncTask的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆