简体   繁体   English

从另一个线程更改UI

[英]Changing UI from another thread

UPDATED: I updated it seith some logs inputs, than you for your help. 更新:我更新了一些日志输入,而不是您的帮助。

I'm applying the following code: 我正在应用以下代码:

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

}   

The result of the logcat is as follows: logcat的结果如下:

D/EthernetActivity(15721): open SUCCESS D / EthernetActivity(15721):打开成功

D/EthernetActivity(15721): status: -1 D / EthernetActivity(15721):状态:-1

D/EthernetActivity(15721): bytes: null D / EthernetActivity(15721):字节:空

so it would seem to me that the problem is in "str_non_final = new String( bytes, "UTF-8");". 因此在我看来,问题出在“ str_non_final = new String(bytes,“ UTF-8”);“。 What is wrong with this? 这有什么问题?

in this statement 在此声明中

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

you are using byte and which is null as printed in your logcat. 您使用的是字节,在logcat中显示为null。 so it will throw the NullPointerException and your catch is able to handle only UnsupportedEncodingException . 因此它将引发NullPointerException并且您的捕获仅能够处理UnsupportedEncodingException

make sure that bytes should not null before call 确保在调用之前bytes不应该为null

write_to_screen( bytes );

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM