首页 > 解决方案 > 错误:无效的方法声明;android 所需的返回类型

问题描述

我有一些麻烦这些代码。我想在蓝牙设备之间发送消息。但是 sendReceiver 函数有一些错误。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();

        }


    }

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

我看起来都是同一个问题。但是没有任何答案。

以及关于 sendreceive 的所有功能。

 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();
        }

    }

}

标签: android

解决方案


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

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


推荐阅读