I need to start the imageview animation, namely the rotate animation in a separate thread. For this, I implemented the Runnable interface:

 @Override public void run() { Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotateanimation); button.startAnimation(animation); //<--- 176 строка Π² Π΄Π΅Π±Π°Π³Π΅ } } 

And of course the animation -

 <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:interpolator="@android:anim/linear_interpolator" > <rotate android:duration="2000" android:fromDegrees="0" android:pivotX="50%" android:pivotY="50%" android:startOffset="0" android:toDegrees="360" /> </set> 

Also implemented the View.OnClickListener interface:

 @Override public void onClick(View v) { switch (v.getId()) { case R.id.toggleGreenLed: try { int value = 0; value = (greenButton.isChecked() ? 1 : 0) + 130; OutputStream outStream = clientSocket.getOutputStream(); outStream.write(value); } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); } break; case R.id.connect: // Π²ΠΎΡ‚ эту ΠΊΠ½ΠΎΠΏΠΊΡƒ Π½Π°ΠΆΠΈΠΌΠ°Π΅ΠΌ, Ρ‡Ρ‚ΠΎΠ±Ρ‹ Π·Π°ΠΏΡƒΡΡ‚ΠΈΡ‚ΡŒ Π°Π½ΠΈΠΌΠ°Ρ†ΠΈΡŽ этой ΠΆΠ΅ ΠΊΠ½ΠΎΠΏΠΊΠΈ Thread thread = new Thread(this) thread.start();//запускаСм ΠΌΠ΅Ρ‚ΠΎΠ΄ run() Toast.makeText(this, "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅...", Toast.LENGTH_LONG); tryConnectToWatch(); //инициализация bluetooth break; } } 

Here is the actual result of the work:

android.view.ViewRoot $ CalledFromWrongThreadException:

To be honest all perelazil, tried ways to eliminate - did not help. Waiting for help, thanks in advance

 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(); Toast.makeText(getApplicationContext(), "БраслСт ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ ΡƒΡΠΏΠ΅ΡˆΠ½ΠΎ.", Toast.LENGTH_LONG).show(); textView.setText("Устройство ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΎ"); } 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()); } } } 

Running an animation and tryConnectTowatch () method

 public void onClick(View v) { switch (v.getId()) { case R.id.toggleGreenLed: try { int value = 0; value = (greenButton.isChecked() ? 1 : 0) + 130; OutputStream outStream = clientSocket.getOutputStream(); outStream.write(value); } catch (IOException e) { Log.d("BLUETOOTH", e.getMessage()); } break; case R.id.connect: runOnUiThread(new Runnable() { @Override public void run() { tryConnectToWatch(); } }); button.startAnimation(animation); Toast.makeText(this, "ΠŸΠΎΠ΄ΠΎΠΆΠ΄ΠΈΡ‚Π΅...", Toast.LENGTH_LONG); } } 

    2 answers 2

    An exception

    android.view.ViewRoot $ CalledFromWrongThreadException:

    says that you are not interacting with the UI from the thread that this UI created, which you cannot do.

    Alternatively, the code that manipulates the UI can be run in the main thread in the following way:

     runOnUiThread(new Runnable() { @Override public void run() { // some actions } }); 
    • It worked, but there is another problem. For some reason, when I click on this button, the bluetooth device initializes first, and then when it is finished - the animation. Why is that? Indeed, in a separate thread was created. - Ker_ Jen
    • Do you want to start the animation in a third-party stream? - post_zeew
    • Otherwise, I do not see a way. Initialization always goes first, no matter where the method calls it is. - Ker_ Jen

    Perhaps you should initialize bluetooth in a separate thread, not an animation ... After all, the initialization of bluetooth can "slow down" the main stream where the animation is going.

    Update

    You need to make your connection to the bracelet in a separate thread, you are now basically there. If you do this, the animation will not fail, just keep in mind that you cannot change your views from the stream

     textView.setText("Устройство ΠΏΠΎΠ΄ΠΊΠ»ΡŽΡ‡Π΅Π½ΠΎ"); 

    so you need to use something either AsyncTask (well, that comes from the box) or RxJava , a good library, which is convenient to organize multithreading.

    • In fact, this is all very strange, the same story. Something is wrong with this initialization. Can she all application completely slows down? - Ker_ Jen
    • and what have you got there? give code example - Hank Moody
    • corrected the question at the very bottom - Ker_ Jen