首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >split()和parseInt

split()和parseInt
EN

Stack Overflow用户
提问于 2015-06-05 17:52:51
回答 1查看 761关注 0票数 0

在创建字符串之后

代码语言:javascript
复制
String data = String.valueOf(seekBar1.getProgress()) + ":"
            + String.valueOf(seekBar2.getProgress()) + ":"
            + String.valueOf(seekBar3.getProgress()) + ":"
            + String.valueOf(seekBar4.getProgress()) + ":"
            + String.valueOf(seekBar5.getProgress());

通过蓝牙将此字符串发送到另一个设备,然后使用

代码语言:javascript
复制
byte[] rBuf = (byte[]) msg.obj;
String rMessage = new String(rBuf);
String[] split = rMessage.split(":");
seekBar1.setProgress(Integer.parseInt(split[0]));
seekBar2.setProgress(Integer.parseInt(split[1]));
seekBar3.setProgress(Integer.parseInt(split[2]));
seekBar4.setProgress(Integer.parseInt(split[3]));
seekBar5.setProgress(Integer.parseInt(split[4]));

我得到了

代码语言:javascript
复制
java.lang.NumberFormatException: Invalid int: "0�������������"...

我知道异常是什么,但我不完全确定为什么要抛出它,我已经将接收到的数字打印成字符串,据我所能看到,只有整数存在于split4中。

你知道这是怎么回事吗?可能和InputStream /OutputStream有关.

处理蓝牙连接I/O的线程。

代码语言:javascript
复制
private class ConnectedThread extends Thread {
        private final BluetoothSocket mmSocket;
        private final InputStream mmInStream;
        private final OutputStream mmOutStream;

        public ConnectedThread(BluetoothSocket socket, String socketType) {
            Log.d(TAG, "create ConnectedThread: " + socketType);
            mmSocket = socket;
            InputStream tmpIn = null;
            OutputStream tmpOut = null;

            // Get the BluetoothSocket input and output streams
            try {
                tmpIn = socket.getInputStream();
                tmpOut = socket.getOutputStream();
            } catch (IOException e) {
                Log.e(TAG, "temp sockets not created", e);
            }

            mmInStream = tmpIn;
            mmOutStream = tmpOut;
        }

        public void run() {
            Log.i(TAG, "BEGIN mConnectedThread");
            byte[] buffer = new byte[1024];
            int bytes;

            // Keep listening to the InputStream while connected
            while (true) {
                try {
                    // Read from the InputStream
                    bytes = mmInStream.read(buffer);

                    // Send the obtained bytes to the UI Activity
                    mHandler.obtainMessage(ControlActivity.MESSAGE_READ, bytes, -1, buffer)
                            .sendToTarget();
                } catch (IOException e) {
                    Log.e(TAG, "disconnected", e);
                    connectionLost();
                    break;
                }
            }
        }

        /**
         * Write to the connected OutStream.
         * @param buffer  The bytes to write
         */
        public void write(byte[] buffer) {
            try {
                mmOutStream.write(buffer);

                // Share the sent message back to the UI Activity
                mHandler.obtainMessage(ControlActivity.MESSAGE_WRITE, -1, -1, buffer)
                        .sendToTarget();
            } catch (IOException e) {
                Log.e(TAG, "Exception during write", e);
            }
        }

        public void cancel() {
            try {
                mmSocket.close();
            } catch (IOException e) {
                Log.e(TAG, "close() of connect socket failed", e);
            }
        }
    }
EN

回答 1

Stack Overflow用户

发布于 2015-06-05 18:35:09

可能rMessage在结尾有一些字符,比如EOL字符或其他什么的。

如果您不想花时间调试you could add a ":" to the end of "data"

代码语言:javascript
复制
String data = String.valueOf(seekBar1.getProgress()) + ":"
            + String.valueOf(seekBar2.getProgress()) + ":"
            + String.valueOf(seekBar3.getProgress()) + ":"
            + String.valueOf(seekBar4.getProgress()) + ":"
            + String.valueOf(seekBar5.getProgress()) + ":";

这样,您的split[4]仍然是正确的,并且只有split[5]将是无效的,您无论如何都不会使用它。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/30672851

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档