2

这是代码

变量声明

 public String gpu2dcurent = "1234567";

Asynctask,完成后它应该更新变量gpu2dcurent,但它没有

 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

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

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

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";
4

4 回答 4

2

在 gpu 类中声明如下:

public String gpu2dcurent = "1234567";

在 AsyncTask 类 readgpu2d 中,使用如下:

gpu.gpu2dcurent = result;

更新你的 UI=User Interface 意味着 TextView 在

onProgressUpdate()

AsyncTask 的方法。

And check where have you declared gpu2dcurent variable???
于 2012-08-01T09:47:53.823 回答
2

我怀疑您是否尝试TextView 在完成异步任务之前设置文本。

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

TextView或者,设置文本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();
于 2012-08-01T09:49:50.820 回答
2

尝试将 setText 放入onPostExecute

protected void onPostExecute(String result) {
     TextView tx = (TextView)findViewById(R.id.textView3);
     tx.setText(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();
     }
 }
于 2012-08-01T09:55:05.423 回答
0

这是整个代码

 package rs.pedjaapps.DualCore;

 import java.io.BufferedReader;
 import java.io.File;
 import java.io.FileInputStream;
 import java.io.IOException;
 import java.io.InputStreamReader;
 import java.util.List;
 import java.util.concurrent.TimeoutException;
 import android.app.Activity;
 import android.app.ProgressDialog;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.util.Log;
 import android.view.View;
 import android.widget.AdapterView;
 import android.widget.ArrayAdapter;
 import android.widget.Spinner;
 import android.widget.AdapterView.OnItemSelectedListener;
 import android.widget.TextView;
 import android.widget.Toast;

 import com.stericson.RootTools.RootTools;
 import com.stericson.RootTools.RootToolsException;

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";

private ProgressDialog pd = null;
private Object data = null;

 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";
            }

            //gpu2dcurent = aBuffer.trim();
            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();
         }

     }

    }




    @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gpu);
this.pd = ProgressDialog.show(this, "Working..", "Loading...", true, false);
new readgpu2d().execute("blablabla");
TextView tx = (TextView)findViewById(R.id.textView3);
tx.setText(gpu2dcurent);
 }


}
于 2012-08-01T10:08:23.710 回答