這裏更新變量是代碼的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);
我錯過了什麼?當我更新的TextView從onPostExecute方法裏面工作正常,但當的AsyncTask完成變量值重置爲默認
public class gpu extends Activity{
public String gpu2dcurent = "1234567";
正如我告訴你,你**正在努力在完成AsyncTask **之前獲得值.. – user370305 2012-08-01 10:10:30
因此,在使用'gpu2dcurent'的值之前,請等待完成您的AsyncTask。使用AsyncTask的'get()'方法。 – user370305 2012-08-01 10:12:11
看看我更新的答案.. – user370305 2012-08-01 10:14:53