2

ここにコードがあります

宣言された変数

 public String gpu2dcurent = "1234567";

Asynctask、終了後、変数 gpu2dcurrent を更新する必要がありますが、更新しません

 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

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 に答える
2

gpu クラスでは、次のように宣言します。

public String gpu2dcurent = "1234567";

AsyncTask クラス readgpu2d では、次のように使用します。

gpu.gpu2dcurent = result;

UIを更新する=ユーザーインターフェイスはTextViewを意味します

onProgressUpdate()

AsyncTask のメソッド。

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

Asynctask が完了する前にテキストを設定しようとしている疑いがあります。TextView

はい、成功するか、static public String gpu2dcurent = "1234567";

または、テキストをTextViewinに設定します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 に答える
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 に答える