Android, java - Unable to update variable from asynctask

后端 未结 4 1007
别那么骄傲
别那么骄傲 2021-02-11 04:39

Here is the code

Variable declared

 public String gpu2dcurent = \"1234567\";

Asynctask, after finished it should update variable gpu2dc

相关标签:
4条回答
  • 2021-02-11 05:18

    In gpu class declare it as followin:

    public String gpu2dcurent = "1234567";
    

    In AsyncTask class readgpu2d, use it as following:

    gpu.gpu2dcurent = result;
    

    Update your UI=User Interface means TextView in

    onProgressUpdate()

    method of AsyncTask.

    And check where have you declared gpu2dcurent variable???
    
    0 讨论(0)
  • 2021-02-11 05:23

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

    Yes, either make it, static public String gpu2dcurent = "1234567";

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

    Update:

    After update your code in question,

    change this line,

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

    to

    new readgpu2d().execute("blablabla").get();
    
    0 讨论(0)
  • 2021-02-11 05:31

    try to put setText into 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();
         }
     }
    
    0 讨论(0)
  • 2021-02-11 05:34

    Here is the whole code

     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);
     }
    
    
    }
    
    0 讨论(0)
提交回复
热议问题