2014-03-05 37 views
0

當我點擊一個項目在我的列表視圖,我想有一個新的線程啓動並執行我在Connect2類中定義的所需任務,但是,作爲據我所知,線程沒有啓動或顯示爲什麼沒有啓動。如果有人能夠幫助診斷代碼中的問題,或者指出我正確解決這個問題,我將不勝感激。謝謝!公共類實現runnable將無法啓動Android

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





    final ListView fileDirectory=(ListView) findViewById(R.id.DirectoryView); 
    final ArrayAdapter adapter=new ArrayAdapter(this, android.R.layout.simple_list_item_1, SocketConnection.remoteList); 
    fileDirectory.setAdapter(adapter); 


    fileDirectory.setOnItemClickListener(new AdapterView.OnItemClickListener() { 
     @Override 
     public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) { 
      Toast.makeText(FileDirectory.this, "The click works", Toast.LENGTH_SHORT).show(); 
      FileDirectory.listItem=i; 
      new Connect2().execute(""); 

      adapter.notifyDataSetChanged(); 


     } 
    }); 
} 
public class Connect2 extends AsyncTask<String, String, String>{ 
    @Override 
    protected String doInBackground(String... params){ 
     runConnectCode(); 
     return null; 
    } 
} 
public void runConnectCode(){ 
     String itemTemp=SocketConnection.remoteList.toArray()[FileDirectory.listItem].toString(); 

      SocketConnection.PW.write(itemTemp); 
      //SocketConnection.DOS.flush(); 


      SocketConnection.currentF.concat("\\"+itemTemp); 
      SocketConnection.PW.write(SocketConnection.currentF); 
      //SocketConnection.DOS.flush(); 
     try{ 
      Object object=SocketConnection.OIS.readObject(); 
      SocketConnection.remoteList=(ArrayList<String>) object; 
     } catch(IOException e){ 
      System.out.println(e); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
    }//run() 
+1

'System.out'沒有按」不一定要去任何地方。使用'System.err'或者更好的'android.util.Log'' – laalto

回答

0

嘗試使用runOnUiThread()運行您的線程;

+0

如果我在UI線程上運行它,它會停止並強制程序關閉。這就是爲什麼即時通訊嘗試在單獨的後臺線程上運行它。 – user3385039

+0

使用Asynctask怎麼樣?! –

+0

我不完全理解異步任務如何工作,我將如何設置這種情況? – user3385039

0

嘗試創建類擴展的AsyncTask,並呼籲在doinbackground代碼()方法是這樣的:

public class Connect2 extends AsyncTask<String , String , String> 

{ 

    @Override 
    protected String doInBackground(String... params) { 

     runConnectCode(); 
     return null; 
    } 

} 

public void runConnectCode() 
{ 
    String itemTemp = SocketConnection.remoteList.toArray() [FileDirectory.listItem].toString(); 
    try { 
     SocketConnection.DOS.writeUTF(itemTemp); 
     SocketConnection.DOS.flush(); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 
    try { 
     SocketConnection.currentF.concat("\\" + itemTemp); 
     SocketConnection.DOS.writeUTF(SocketConnection.currentF); 
     SocketConnection.DOS.flush(); 
    } catch (IOException e) { 
     System.out.println(e); 
    } 
    try { 
     Object object = SocketConnection.OIS.readObject(); 
     SocketConnection.remoteList = (ArrayList<String>) object; 
    } catch (IOException e) { 
     System.out.println(e); 
    } catch (ClassNotFoundException e) { 
     e.printStackTrace(); 
    } 

    } 
} 

最後,調用這個只要你想:

new Connect2().execute(""); 
+0

與以前一樣的問題。線程似乎不會開始 – user3385039

+0

你可以在添加AsynckTask後發佈你的代碼嗎? @ user3385039 –

+0

更新了原始代碼 – user3385039