Android,java - 无法从asynctask更新变量 [英] Android, java - Unable to update variable from asynctask

查看:118
本文介绍了Android,java - 无法从asynctask更新变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是代码

变量声明

 public String gpu2dcurent = "1234567";

Asynctask,完成之后应该更新变量gpu2dcurent,但它不会

Asynctask, after finished it should update variable gpu2dcurent, but it doesnt

 private class readgpu2d extends AsyncTask<String, Void, String> {


    protected String doInBackground(String... args) {
         Log.i("MyApp", "Background thread starting");

         String aBuffer = "";

         try {

            File myFile = new File("/sys/devices/platform/kgsl-2d0.0/kgsl/kgsl-2d0/gpuclk");
            FileInputStream fIn = new FileInputStream(myFile);
            BufferedReader myReader = new BufferedReader(
                    new InputStreamReader(fIn));
            String aDataRow = "";
            //String aBuffer = "";
            while ((aDataRow = myReader.readLine()) != null) {
                aBuffer += aDataRow + "\n";
            }

            ;
            myReader.close();

        } catch (Exception e) {
            Toast.makeText(getBaseContext(), e.getMessage(),
                    Toast.LENGTH_SHORT).show();

        }

         return aBuffer.trim();
     }

     protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show();
         gpu.this.data = result;

         if (gpu.this.pd != null) {
             //gpu.this.pd.dismiss();
         }
     }

    }

测试到查看变量是否具有新值。它没有,它显示1234567

Test to see if a variable has a new value. It doesn't, it displays 1234567

 TextView tx = (TextView)findViewById(R.id.textView3);
tx.setText(gpu2dcurent);

我错过了什么吗?当我从onPostExecute方法内部更新textView时它工作正常,但是当Asynctask完成变量值重置为默认值时

Am i missing something? When i update textView from inside onPostExecute method it works fine but when Asynctask finishes variable value resets to default

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";


推荐答案

我怀疑你是否正在尝试设置文本 TextView 在完成Asynctask之前。

I have a doubt you are trying to set text of TextView before completion of your Asynctask.

是的,要么成功, static public String gpu2dcurent =1234567;

或者,设置Text of TextView in onPostExecute()

Or, set Text of TextView in onPostExecute()

protected void onPostExecute(String result) {
         // Pass the result data back to the main activity
        gpu2dcurent = result;
         //Toast.makeText(getBaseContext(), result,
                //Toast.LENGTH_SHORT).show();
         gpu.this.data = result;  
         if (gpu.this.pd != null) {
             //gpu.this.pd.dismiss();
         }
      tx.setText(gpu2dcurent);
     }
    }

更新:

更新您的相关代码后,

更改此行,

new readgpu2d().execute("blablabla");

new readgpu2d().execute("blablabla").get();

这篇关于Android,java - 无法从asynctask更新变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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