By pressing the button, the initialization of the bluetooth module (hc-06) should start and simultaneously with it the animation of this button. I was offered to use the AsyncTask class.
Here is the implementation code for the AsyncTask interface -
class BluetoothConnect extends AsyncTask<Void, Void, Void> { @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); MainActivity mainActivity = new MainActivity(); mainActivity.textView.setText("Подключено"); //та самая 181 строка } @Override protected Void doInBackground(Void... params) { tryConnectToWatch(); return null; } } } This is the module initialization -
public void tryConnectToWatch() { if (bluetooth.isEnabled()) { try { BluetoothDevice device = bluetooth.getRemoteDevice("20:16:08:16:14:57"); Method m = device.getClass().getMethod("createRfcommSocket", new Class[]{int.class}); clientSocket = (BluetoothSocket) m.invoke(device, 1); clientSocket.connect(); } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); } catch (SecurityException e) { Log.d("BLUETOOTH", e.getMessage()); } catch (NoSuchMethodException e) { Log.d("BLUETOOTH", e.getMessage()); } catch (IllegalArgumentException e) { Log.d("BLUETOOTH", e.getMessage()); } catch (IllegalAccessException e) { Log.d("BLUETOOTH", e.getMessage()); } catch (InvocationTargetException e) { Log.d("BLUETOOTH", e.getMessage()); } } } But the button click handler
case R.id.connect: button.startAnimation(animation); Toast.makeText(this, "Подождите...", Toast.LENGTH_LONG); BluetoothConnect bluetoothConnect = new BluetoothConnect(); bluetoothConnect.execute(); After clicking, the animation and initialization starts, but an exception flies to this line -
mainActivity.textView.setText("Подключено"); //181 строка, помечена в коде выше Exception - java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.TextView.setText (java.lang.CharSequence)' on a null object reference
Help solve the problem. Thank you in advance