I have this method

public synchronized void setProgress(final int progress, final boolean fromUser) { if (mMethodSetProgress == null) { try { Method m; m = this.getClass().getMethod("setProgress", int.class, boolean.class); m.setAccessible(true); mMethodSetProgress = m; } catch (NoSuchMethodException ignored) { } } if (mMethodSetProgress != null) { new Thread(new Runnable() { @Override public void run() { try { mMethodSetProgress.invoke(this, progress, fromUser); } catch (IllegalAccessException | InvocationTargetException e) { e.printStackTrace(); } } }).start(); } else { super.setProgress(progress); } refreshThumb(); } 

and for some reason I'm flying out to catch() with an error

 FATAL EXCEPTION: Thread-7222 Process: com.example.android.camera2basic, PID: 20402 java.lang.IllegalArgumentException: Expected receiver of type com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar, but got com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar$1 at java.lang.reflect.Method.invoke(Native Method) at java.lang.reflect.Method.invoke(Method.java:372) at com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar$1.run(VerticalSeekBar.java:310) at java.lang.Thread.run(Thread.java:818) 

Tell me what's wrong?

Addition

 android.widget.ProgressBar.invalidateDrawable(ProgressBar.java:1673) 06-30 18:37:11.048 8729-10663/com.example.android.camera2basic W/System.err: at android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:408) 06-30 18:37:11.048 8729-10823/com.example.android.camera2basic W/System.err: at com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar$1.run(VerticalSeekBar.java:309) 06-30 18:37:11.048 8729-10823/com.example.android.camera2basic W/System.err: at java.lang.Thread.run(Thread.java:818) 06-30 18:37:11.048 8729-10776/com.example.android.camera2basic W/System.err: Caused by: at 06-30 18:37:11.048 8729-10472/com.example.android.camera2basic W/System.err: Caused by: at 06-30 18:37:11.048 8729-10840/com.example.android.camera2basic W/System.err: at 06-30 18:37:11.048 8729-10840/com.example.android.camera2basic W/System.err: at com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar$1.run(VerticalSeekBar.java:309) 06-30 18:37:11.058 8729-10731/com.example.android.camera2basic W/System.err: android.widget.ProgressBar.invalidateDrawable(ProgressBar.java:1673) 06-30 18:37:11.058 8729-10731/com.example.android.camera2basic W/System.err: at android.view.ViewGroup.invalidateChild(ViewGroup.java:4946) 06-30 18:37:11.058 8729-10731/com.example.android.camera2basic W/System.err: at android.view.View.invalidateInternal(View.java:12576) 06-30 18:37:11.058 8729-10731/com.example.android.camera2basic W/System.err: at android.view.View.invalidate(View.java:12512) 06-30 18:37:11.058 8729-10735/com.example.android.camera2basic W/System.err: at at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7062) 06-30 18:37:11.058 8729-10735/com.example.android.camera2basic W/System.err: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1022) 06-30 18:37:11.058 8729-10753/com.example.android.camera2basic W/System.err: android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7062) at 06-30 18:37:11.058 8729-10753/com.example.android.camera2basic W/System.err: at android.view.ViewRootImpl.invalidateChildInParent(ViewRootImpl.java:1022) 06-30 18:37:11.058 8729-10753/com.example.android.camera2basic W/System.err: at android.view.ViewGroup.invalidateChild(ViewGroup.java:4946) 06-30 18:37:11.058 8729-10730/com.example.android.camera2basic W/System.err: at com.example.android.camera2basic.tools.verticalseekbar.VerticalSeekBar$1.run(VerticalSeekBar.java:309) at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:7062)android.graphics.drawable.Drawable.invalidateSelf(Drawable.java:408) 
  • Please bring the logs as a code and not as a quote, it is inconvenient to read - xkor
  • @xkor approx. It seemed to me more comfortable) - Aleksey Timoshchenko

1 answer 1

Because in the line mMethodSetProgress.invoke(this, progress, fromUser); you will have an anonymous runnable implementation in this and not a VerticalSeekBar. It should be like this: mMethodSetProgress.invoke(VerticalSeekBar.this, progress, fromUser);

The fact is that when you write new Runnable() { you start writing the implementation of the nested anonymous class inherited from Runnable and, accordingly, while you are inside this implementation, the this keyword will be associated with this nested class. The VerticalSeekBar.this entry indicates that you want to refer to the current instance of VerticalSeekBar. Such a record only works in the classes nested in VerticalSeekBar, well, still in itself, but there it is meaningless.

  • Yes, it is ... I really did not understand why, but it worked ... Well, I mean, this error doesn’t show up now, but now I seemed to add another to the question, can you tell me about it? - Aleksey Timoshchenko
  • Something in my new error I do not see what error message is, only its frame. Yes, and that which is pokotsany something - xkor
  • Damn, well, there is no fatal error as such, but in the end the method does not work ... Then you probably need to look at the code completely ... Okay, I will continue to test, I think I’ll find what is wrong - Aleksey Timoshchenko