2013-03-05 30 views
0

更新:我更新了seith一些日誌輸入,比你的幫助。從另一個線程更改用戶界面

我申請以下代碼:

@Override 
public void run() { 

    byte[] bytes = null; 

    int TIMEOUT = 5000; 

    int status = connection.bulkTransfer(ep, bytes, ep.getMaxPacketSize(), TIMEOUT); 

    Log.d(TAG, "status: " + status); 

    write_to_screen(bytes); 

} 

public void write_to_screen(byte[] bytes ) { 

    Log.d(TAG, "bytes: " + bytes); 

    String str_non_final = null; 

    try { 

     str_non_final = new String(bytes, "UTF-8"); 
     Log.d(TAG, "str_non_final : " + str_non_final); 

    } catch (UnsupportedEncodingException e) { 

     e.printStackTrace(); 

    } 

    final String str = str_non_final; 

    Log.d(TAG, "str: " + str); 

    runOnUiThread(new Runnable() {@Override public void run() 
    { 
     textView7.setText(str); 
     Log.d(TAG, "UI updated"); 
    } 
    }); 

} 

的logcat的結果如下:

d/EthernetActivity(15721):開放SUCCESS

d/EthernetActivity(15721 ):status:-1

D/EthernetActivity(15721):bytes:null

所以在我看來,問題出在「str_non_final = new String(bytes,」UTF-8「);」。這有什麼問題?

+1

之前,還請從logcat的發佈崩潰的堆棧跟蹤。 – Thrakbad 2013-03-05 09:40:14

+0

@ user2115240請添加LogCat – 2013-03-05 09:43:58

回答

1

在此聲明

str_non_final = new String(bytes, "UTF-8"); 

您使用的字節,這是空印在你的logcat中。所以它會拋出NullPointerException,你的漁獲物只能處理UnsupportedEncodingException

確保bytes不應空呼

write_to_screen(bytes);