2014-02-23 37 views
-1

怎麼回事是代碼:使用Twitter的API創建Android應用程序,控制不線程

public void onClick(View v) { 
    Log.d(TAG, "hay i am working till here"); 

    new Thread() { 

     public void run() { 
      try { 
       Log.d(TAG, "hay i reached till here"); 
       twitter = new Twitter("student", "password"); 
       twitter.setAPIRootUrl("http://yamba.marakana.com/api"); 
       twitter.setStatus(editText.getText().toString()); 
       Log.d(TAG,"Successfully Posted"); 
      } catch (TwitterException e) { 
       Log.e(TAG, "Died", e); 
      } 
     } 
    }.start(); 
    Log.d(TAG, "onClicked"); 

} 

當我運行它,它只是達到第一個日誌即Log.d(TAG, "hay i am working till here");我無法找出什麼問題是。

+0

但你的問題是什麼? –

回答

0

問題在於這樣一個事實:你想通過非UI線程與此行訪問UI線程代碼中的

twitter.setStatus(editText.getText().toString()); 

由於edittext.getText()應訪問/呼籲UI線程。

而是在UI線程上使用處理程序來完成您的需求。

Handler tweethandler=new Handler(getMainLooper()); 

tweethandler。員額(新的Runnable(){ 公共無效的run(){

  try { 
       Log.d(TAG, "hay i reached till here"); 
       twitter = new Twitter("student", "password"); 
       twitter.setAPIRootUrl("http://yamba.marakana.com/api"); 
       twitter.setStatus(editText.getText().toString()); 
       Log.d(TAG,"Successfully Posted"); 
      } catch (TwitterException e) { 
       Log.e(TAG, "Died", e); 
      } 
} 

}); 
+0

對不起,因爲我通過張貼這樣的壞格式手機 – cafebabe1991

+0

試過這個,但這個錯誤似乎在殺我讓我發佈整個事情,我早些時候嘗試在UI線程... – jack

+0

現在不知怎麼我不能發佈這個格式問題 – jack

0
public void onClick(View v) { 
     Log.d(TAG, "hay i am not working"); 
     Handler.post(new Runnable() { 
      public void run() { 

       try { 
        Log.d(TAG, "hay i reached till here"); 
        twitter = new Twitter("student", "password"); 
        twitter.setAPIRootUrl("http://yamba.marakana.com/api"); 
        twitter.setStatus(editText.getText().toString()); 
        Log.d(TAG, "Successfully Posted"); 
       } catch (TwitterException e) { 
        Log.e(TAG, "Died", e); 
       } 
      } 

     }); 
     Log.d(TAG, "onClicked"); 

    } 
相關問題