Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/380.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android,java-无法从asynctask更新变量_Java_Android_Android Asynctask - Fatal编程技术网

Android,java-无法从asynctask更新变量

Android,java-无法从asynctask更新变量,java,android,android-asynctask,Java,Android,Android Asynctask,这是密码 声明的变量 public String gpu2dcurent = "1234567"; Asynctask,完成后应该更新变量gpu2dcurent,但它没有 private class readgpu2d extends AsyncTask<String, Void, String> { protected String doInBackground(String... args) { Log.i("MyApp", "Backgrou

这是密码

声明的变量

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

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

 public class gpu extends Activity{

public String gpu2dcurent = "1234567";

在gpu类中声明如下:

public String gpu2dcurent = "1234567";
在AsyncTask类readgpu2d中,按如下方式使用它:

gpu.gpu2dcurent = result;

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

onProgressUpdate()

异步任务的方法

And check where have you declared gpu2dcurent variable???

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

是,或者设置为,
静态公共字符串gpu2dcurent=“1234567”

或者,在
onPostExecute()

更新:

更新相关代码后

换一行,

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


尝试将setText放入
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);
     }
    }
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();
     }
 }

这是全部代码

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


}
包rs.pedjaapps.DualCore;
导入java.io.BufferedReader;
导入java.io.File;
导入java.io.FileInputStream;
导入java.io.IOException;
导入java.io.InputStreamReader;
导入java.util.List;
导入java.util.concurrent.TimeoutException;
导入android.app.Activity;
导入android.app.ProgressDialog;
导入android.os.AsyncTask;
导入android.os.Bundle;
导入android.util.Log;
导入android.view.view;
导入android.widget.AdapterView;
导入android.widget.ArrayAdapter;
导入android.widget.Spinner;
导入android.widget.AdapterView.OnItemSelectedListener;
导入android.widget.TextView;
导入android.widget.Toast;
导入com.stericson.RootTools.RootTools;
导入com.stericson.RootTools.RootToolsException;
公共类gpu扩展活动{
公共字符串gpu2dcurent=“1234567”;
private ProgressDialog pd=null;
私有对象数据=null;
私有类readgpu2d扩展异步任务{
受保护的字符串doInBackground(字符串…args){
Log.i(“MyApp”,“后台线程启动”);
字符串aBuffer=“”;
试一试{
File myFile=新文件(“/sys/devices/platform/kgsl-2d0.0/kgsl/kgsl-2d0/gpuclk”);
FileInputStream fIn=新的FileInputStream(myFile);
BufferedReader myReader=新BufferedReader(
新的InputStreamReader(fIn));
字符串aDataRow=“”;
//字符串aBuffer=“”;
而((aDataRow=myReader.readLine())!=null){
aBuffer+=aDataRow+“\n”;
}
//gpu2dcurent=aBuffer.trim();
myReader.close();
}捕获(例外e){
Toast.makeText(getBaseContext(),e.getMessage(),
吐司。长度(短)。show();
}
返回附件trim();
}
受保护的void onPostExecute(字符串结果){
//将结果数据传回主活动
gpu2dcurent=结果;
//Toast.makeText(getBaseContext(),result,
//吐司。长度(短)。show();
gpu.this.data=结果;
如果(gpu.this.pd!=null){
gpu.this.pd.discouse();
}
}
}
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.gpu);
this.pd=ProgressDialog.show(这是“正在工作…”、正在加载…”、true、false);
新建readgpu2d().execute(“blablabla”);
TextView tx=(TextView)findViewById(R.id.textView3);
tx.setText(gpu2dcurent);
}
}

我知道这一点,而且这样做很好,但我需要的是将全局vaiable的值设置为asyncaskIn的结果,您在哪个类中声明了变量gpu2dcurent?在哪个类中声明了变量gpu2dcurent?我不需要更新TextView,我不需要它,只是为了查看变量是否已更改,我需要的是从AsyncTaskBuddy中更新变量使用静态字符串gpu2dcurent variable,它仍然显示默认值只需更改此行,
new readgpu2d().execute(“blablabla”)
to
new readgpu2d().execute(“blablabla”).get()我不需要更新TextView,我不需要它,它只是为了让我可以看到变量是否已更改,我需要的是从AsyncTask中更新变量正如我告诉您的,您正在尝试在完成异步任务之前获取值。因此,在使用
gpu2dcurent
的值之前,请等待AsyncTask完成。使用AsyncTask的
get()
方法。仍然相同。我不明白我做错了什么,你得等任务完成。在完成AsyncTask之前,必须调用它的
.get()
方法,然后使用
gpu2dcurent
变量。
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();
     }
 }
 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);
 }


}