错误:方法声明无效;Android 需要返回类型



我在这些代码上遇到了一些麻烦。我想在蓝牙设备之间发送消息。但是发送接收器函数有一些错误。Erorline实际上是:

public sendReceive (BluetoothSocket socket){
bluetoothSocket=socket;
InputStream tempIn =null;
OutputStream tempOut=null;
try {
tempIn=bluetoothSocket.getInputStream();
tempOut=bluetoothSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

}

我的问题是: 错误:方法声明无效;需要返回类型

我看都是同样的问题。但是没有任何答案。

以及关于发送接收的所有功能。

private class  SendReceive extends Thread{

private final BluetoothSocket bluetoothSocket;
private final InputStream inputStream;
private final OutputStream outputStream;


public sendReceive (BluetoothSocket socket){
bluetoothSocket=socket;
InputStream tempIn =null;
OutputStream tempOut=null;
try {
tempIn=bluetoothSocket.getInputStream();
tempOut=bluetoothSocket.getOutputStream();
} catch (IOException e) {
e.printStackTrace();
}

}

public void run(){
byte[] buffer=new byte[1024];
int bytes;
while(true){
try {
bytes=inputStream.read(buffer);
handler.obtainMessage(STATE_MESSAGE_RECEIVER,bytes,-1,buffer).sendToTarget();
} catch (IOException e) {
e.printStackTrace();
}
}
}
public void write(byte[] bytes){
try {
outputStream.write(bytes);
} catch (IOException e) {
e.printStackTrace();
}
}

}

构造函数名称不正确。而不是sendReceive使用SendReceive.

由于您已将InputStream inputStream&OutputStream outputStream声明为final,因此要么在构造函数中初始化它们,要么删除final。如果在类级别不需要它们,则只需删除它们并在方法级别使用它即可。

最新更新