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";
# 1 楼答案
尝试将setText放入
onPostExecute
# 2 楼答案
在gpu类中声明如下:
在AsyncTask类readgpu2d中,按如下方式使用它:
更新您的UI=用户界面意味着
onProgressUpdate()
异步任务的方法
# 3 楼答案
这是全部代码
# 4 楼答案
我怀疑您是否试图在完成任务之前设置
TextView
的文本强>是的,要么去做,
static public String gpu2dcurent = "1234567";
或者,在
onPostExecute()
中设置TextView
的文本更新:
更新相关代码后
换一行,
到