cuốn sách gpt4 ai đã làm

android - inputStream.read() gây ra NullPointerException (sau khi kiểm tra inputStream !=null)

In lại Tác giả: Taklimakan Thời gian cập nhật: 2023-11-02 22:26:21 29 4
mua khóa gpt4 Nike

Tôi đang viết một ứng dụng cần trao đổi dữ liệu với thiết bị Bluetooth 2.1. Tôi đã làm điều đó nhiều lần, nhưng lần này có điều gì đó kỳ lạ đã xảy ra.

    Log.d("TAG", "connectToDevice");

if(macAddress != null)
deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

Log.d("TAG", "macAddress != null");


if(deviceToConnect != null)
thử {
btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
} catch (IOException e) {
btSocket = null;
e.printStackTrace();
}

Log.d("TAG", "deviceToConnect != null");

if(btSocket != null){

thử {
inputStream = btSocket.getInputStream();
Log.d("TAG", "inputStream OK");

} catch (IOException e) {
inputStream = null;
Log.d("TAG", "inputStream KO");
e.printStackTrace();
}

thử {
đầu raStream = btSocket.getOutputStream();
Log.d("TAG", "outputStream OK");

} catch (IOException e) {
đầu raStream = null;
Log.d("TAG", "outputStream KO");
e.printStackTrace();
}

}


Log.d("TAG", "btSocket != null");
Log.d("TAG", "onConnectionEstablished");

Sau giai đoạn khám phá, tôi nhận được BluetoothDevice mà tôi cần kết nối, sau đó tôi nhận được các luồng ổ cắm, đầu vào và đầu ra.

Sau đó, tôi có một chuỗi đọc từ luồng đầu vào.

   int byteRead;

trong khi (nghe) {
thử {
if(inputStream!=null){
Log.d("TAG", "inputStream: " +inputStream);
byteRead = inputStream.read();
}khác{
Log.d("TAG", "inputStream là null!");
continue;
} // Phần còn lại của mã ở đây

tôi đang thực hiện inputStream.read() Tôi gặp lỗi này khi chạy:

java.lang.NullPointerException: Cố gắng gọi phương thức ảo 'int java.io.InputStream.read(byte[], int, int)' trên tham chiếu đối tượng null

tại android.bluetooth.BluetoothSocket.read(BluetoothSocket.java:427)
tại android.bluetooth.BluetoothInputStream.read(BluetoothInputStream.java:60)
tại com.me.testapplication.Connection_BT21.run(Connection_BT21.java:152)

Câu hỏi 1: Nếu tôi đang kiểm tra inputStream != null, tại sao tôi lại nhận được NullPointerException?

Câu hỏi 2: Nếu tôi thử gọi read(), tại sao lại đọc(byte[], int, int)?

câu trả lời hay nhất

Tôi đã gặp lỗi, bạn vẫn ổn: ổ cắm có lỗi. Tôi cần gọi socket.connect()!

if(macAddress != null)
deviceToConnect = mBluetoothAdapter.getRemoteDevice(macAddress);

Log.d("TAG", "macAddress != null");


if(deviceToConnect != null)
thử {
btSocket = deviceToConnect.createRfcommSocketToServiceRecord(UUID.fromString(SharedIncludes.SPP_UUID));
} catch (IOException e) {
btSocket = null;
e.printStackTrace();
}

Log.d("TAG", "deviceToConnect != null");

if(btSocket != null){
//Đây là dòng bị thiếu!
btSocket.connect();

thử {
inputStream = btSocket.getInputStream();
Log.d("TAG", "inputStream OK");

} catch (IOException e) {
inputStream = null;
Log.d("TAG", "inputStream KO");
e.printStackTrace();
}

thử {
đầu raStream = btSocket.getOutputStream();
Log.d("TAG", "outputStream OK");

} catch (IOException e) {
đầu raStream = null;
Log.d("TAG", "outputStream KO");
e.printStackTrace();
}

}

Đó là lý do tại sao inputStream chưa sẵn sàng và tôi gặp lỗi đó.

Xin lỗi, tôi chỉ không nhìn thấy nó.. Hy vọng nó sẽ giúp được người khác!

Về android - inputStream.read() gây ra NullPointerException (sau khi kiểm tra inputStream !=null), chúng tôi đã tìm thấy một câu hỏi tương tự trên Stack Overflow: https://stackoverflow.com/questions/24267671/

29 4 0
Chứng chỉ ICP Bắc Kinh số 000000
Hợp tác quảng cáo: 1813099741@qq.com 6ren.com
Xem sitemap của VNExpress