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

    1 answer 1

    MainActivity mainActivity = new MainActivity ();

    So you created a blank activation that is not attached to the system, and did not turn to the one that is now on the screen and it works.

    You need to transfer, for example, your activity to the class and work with it.

     class BluetoothConnect extends AsyncTask<Void, Void, Void> { MainActivity activity; public BluetoothConnect(MainActivity activity) { this.activity = activity; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); activity .textView.setText("Подключено"); //та самая 181 строка } @Override protected Void doInBackground(Void... params) { tryConnectToWatch(); return null; } } } 

    Call this:

     BluetoothConnect bluetoothConnect = new BluetoothConnect(this); bluetoothConnect.execute(); 
    • @Ker_Jen, supplemented the answer - YuriySPb
    • Hmm, I didn’t think of using a designer, now I’ll try ... - Ker_ Jen
    • one
      Perfectly! Everything is working. Thank. After 4 minutes I will post the answer - Ker_ Jen