2012-08-01 27 views
2

這裏更新變量是代碼的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"; 
+0

正如我告訴你,你**正在努力在完成AsyncTask **之前獲得值.. – user370305 2012-08-01 10:10:30

+0

因此,在使用'gpu2dcurent'的值之前,請等待完成您的AsyncTask。使用AsyncTask的'get()'方法。 – user370305 2012-08-01 10:12:11

+0

看看我更新的答案.. – user370305 2012-08-01 10:14:53

回答

2

我中有你正在嘗試你的AsyncTask完成之前設定的TextView文本疑問。

是,要麼做到最好的,static public String gpu2dcurent = "1234567";

或者,在TextView設置文本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); 
    } 
    } 

更新:

更新你的代碼有問題後,

改變這一行,

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

new readgpu2d().execute("blablabla").get(); 
+0

我不需要更新TextView,我不需要它,它只是所以我可以看到如果變量已經被改變或沒有,我需要的是從AsyncTask更新變量 – pedja 2012-08-01 09:56:57

+0

好友使用靜態字符串gpu2dcurent變量, – user370305 2012-08-01 09:57:57

+0

它仍然顯示默認值 – pedja 2012-08-01 10:00:47

2

在GPU類中聲明它跟隨着:

public String gpu2dcurent = "1234567"; 

在的AsyncTask類readgpu2d,用它作爲以下:

gpu.gpu2dcurent = result; 

更新您的UI =用戶接口裝置的TextView在

onProgressUpdate()的AsyncTask的

方法。

And check where have you declared gpu2dcurent variable??? 
+0

我知道,它的工作方式很好,但我需要的是從asyncask設置一個全局有效值的結果 – pedja 2012-08-01 09:50:39

+0

在哪個類中聲明變量** gpu2dcurent * *? – 2012-08-01 09:53:48

+0

更新後的第一篇文章 – pedja 2012-08-01 09:55:47

2

儘量把的setText到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

我不需要更新TextView,我不需要它,它只是所以我可以看到,如果變量已被更改或沒有,我需要的是更新從AsyncTask – pedja 2012-08-01 09:57:16

0

這裏是整個代碼

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


}