抛出OutOfMemoryError" pthread_create(1040KB堆栈)失败:再试一次"

时间:2016-09-02 08:56:39

标签: java android

我正在尝试从外部蓝牙设备读取/写入数据到我的Android应用程序中。在它崩溃之前它运行良好一分钟我收到了最后发布的错误。我认为错误发生在" run()"功能,但我无法弄清楚是什么。

如果有任何帮助,我将不胜感激。

//create new class for connect thread
private class ConnectedThread extends Thread {
    private final InputStream mmInStream;
    private final OutputStream mmOutStream;
    private String readMessage = null;

    //creation of the connect thread
    public ConnectedThread(BluetoothSocket socket) {
        InputStream tmpIn = null;
        OutputStream tmpOut = null;

        try {
            //Create I/O streams for connection
            tmpIn = socket.getInputStream();
            tmpOut = socket.getOutputStream();
        } catch (IOException e) {
            //if you cannot write, close the application
            Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
            finish();
        }

        mmInStream = tmpIn;
        mmOutStream = tmpOut;
    }

    public void run() {
        byte[] buffer = new byte[256];
        int bytes = 0;

        // Keep looping to listen for received messages
        while (true) {
            try {
                bytes = mmInStream.read(buffer); //read bytes  from input buffer
                readMessage = new String(buffer, 0, bytes);
                // Send the obtained bytes to the UI Activity via handler
                bluetoothIn.obtainMessage(handlerState, bytes, -1, readMessage).sendToTarget();
            } catch (IOException e) {
                e.printStackTrace();
                //if you cannot write or read, close the application
                Toast.makeText(getBaseContext(), "ERROR", Toast.LENGTH_LONG).show();
                finish();
                break;
            }
        }
    }
    //write method
    public void write(String input) {
        byte[] msgBuffer = input.getBytes();           //converts entered String into bytes
        try {
            mmOutStream.write(msgBuffer);                //write bytes over BT connection via outstream
        } catch (IOException e) {
            //if you cannot write, close the application
            Toast.makeText(getBaseContext(), "Connection Failure", Toast.LENGTH_LONG).show();
            finish();

        }
    }
}

错误:

 E/art: Throwing OutOfMemoryError "pthread_create (1040KB stack) failed:  Try again"
 E/InputEventReceiver: Exception dispatching input event.
 D/AndroidRuntime: Shutting down VM

0 个答案:

没有答案