有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

Android,java无法从asynctask更新变量

这是密码

声明的变量

 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) 个答案

  1. # 1 楼答案

    尝试将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();
         }
     }
    
  2. # 2 楼答案

    在gpu类中声明如下:

    public String gpu2dcurent = "1234567";
    

    在AsyncTask类readgpu2d中,按如下方式使用它:

    gpu.gpu2dcurent = result;
    

    更新您的UI=用户界面意味着

    onProgressUpdate()

    异步任务的方法

    And check where have you declared gpu2dcurent variable???
    
  3. # 3 楼答案

    这是全部代码

     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);
     }
    
    
    }
    
  4. # 4 楼答案

    我怀疑您是否试图在完成任务之前设置TextView的文本

    是的,要么去做,static public String gpu2dcurent = "1234567";

    或者,在onPostExecute()中设置TextView的文本

    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();