How to call a function with an argument to another function in java

0

I have the following function:

String[] TXbuffer = new String[]{"H","o","l","a"};


public void writeAsync(String bufferx) {

    if ( mSerialPort != null) {

        try {
            mSerialPort.write(bufferx.getBytes(), SERIAL_TIMEOUT);

         } catch (IOException e) {
        }

    } else {
        mTitleTextView.setText("Dispositivo Serial Desconectado!");
    }
}

and I want to call it from another function:

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    int id = item.getItemId();

    if (id == R.id.bt2_TX) {
        writeAsync(TXbuffer);

        return true;
    }

}

Someone can tell me how to do it correctly, because I have an error!

    
asked by W1ll 24.10.2018 в 19:31
source

1 answer

0

create a string arrangement

String[] TXbuffer = new String[]{"H","o","l","a"};

and your method only expects a string

public void writeAsync(String bufferx) {


}

that's why you get an error when calling this method

 writeAsync(TXbuffer);

you have two possible solutions .. depending on what you are waiting to send to the method.

1 sending the correct TXbuffer text;

Remember that you can call arrangements in this way

String valor1 = TXbuffer[0] esto es igual a "H"
String valor2 = TXbuffer[1] esto es igual a "o"
String valor3 = TXbuffer[2] esto es igual a "l"
String valor4 = TXbuffer[3] esto es igual a "a"

and call your method with the value you want

writeAsync(valor1 ); 

2. change your method to accept string arrays

public void writeAsync(String[] bufferx) {

if ( mSerialPort != null) {

        try {
            mSerialPort.write(bufferx.getBytes(), SERIAL_TIMEOUT);

        } catch (IOException e) {
        }

    } else {
        mTitleTextView.setText("Dispositivo Serial Desconectado!");
    }
}
    
answered by 24.10.2018 в 20:23