这是代码
声明的变量
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";
我怀疑您正在尝试设置文本TextView
在完成异步任务之前。
是的,要么做到,static public String gpu2dcurent = "1234567";
或者,设置文本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:
更新有问题的代码后,
改变这一行,
new readgpu2d().execute("blablabla");
to
new readgpu2d().execute("blablabla").get();
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)