2017-07-08 33 views
-2

這是它運行在模擬器的代碼,但它沒有給出實時值我想開發一個實時圖形應用程序,但它不工作

protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    GraphView graph1 = (GraphView)findViewById(R.id.main_graph1); 

    Series = new LineGraphSeries<DataPoint>(); 
    graph1.addSeries(Series); 

在這裏,我去模擬與線程實時該附加數據:

protected void onResume(){ 
    super.onResume(); 

    new Thread(new Runnable() { 

     public void run() { 

      for(int i = 0; i<100; i++); 
      runOnUiThread(new Runnable() { 

       public void run() { 
        addEntry(); 
       } 
      }); 

      try { 
       Thread.sleep(600); 
      } catch (InterruptedException e) { 

       e.printStackTrace(); 
      } 


     } 


    }); 
} 

我覺得這好像是錯了,請幫忙

+0

你忘了叫'start'和';'爲 –

+0

後,我沒有得到它。你能澄清你說的話嗎? – Hamid7497

回答

0

問題:

1)線程將只當你調用你的線程對象start功能(這是失蹤)

2)for(int i = 0; i<100; i++);這個循環不會做任何事情,由於身體終止由;

執行

這樣的解決辦法是

new Thread(new Runnable() { 
    public void run() { 
     for(int i = 0; i<100; i++) // ; removed 
     runOnUiThread(new Runnable() { 

      public void run() { 
       addEntry(); 
      } 
     }); 
     try { 
      Thread.sleep(600); 
     } catch (InterruptedException e) { 

      e.printStackTrace(); 
     } 
    } 
}).start(); // invoke start for execution 
+0

非常感謝。它的工作,現在它顯示實時價值 – Hamid7497

+0

我很高興,我可以幫助 –

相關問題