Android Java: “Cannot resolve” in postDelayed

こ雲淡風輕ζ 提交于 2020-02-08 04:16:09

问题


I am using the first answer here to try to initiate a repeating task, updating a seekbar ("timeSlider") to show progress as an audio file plays. The repeating function is updateTimeSlider(). When initiated via postDelayed, updateTimeSlider gives a "cannot resolve symbol" error (see image at the bottom of this post). updateTimeSlider() does not give that error when on a line by itself (also shown in the image).

(Note that updateTimeSlider() on the line by itself is not really how the code will go. It's just there to demonstrate that it works in that position.)

Can anyone help me understand how to correct the problem in the postDelayed line?

Several people have suggested that I just forgot to write the do in doUpdateTimeSlider. I did not forget. I wanted to execute updateTimeSlider (without the do), which does execute just fine and does not produce an error outside of postDelayed. updateTimeSlider is a C++ function used via JNI (Java Native Interface) using a statement at the bottom of this Java file that looks like this:

private native void updateTimeSlider();

The JNI aspect of things is working fine, and updateTimeSlider() is the method I am trying to execute, not doUpdateTimeSlider.

As an aside, I did try putting doUpdateTimeSlider in there, and it also results in an error: Variable doUpdateTimeSlider may not have been initialized.

I also don't see what good it would do me if I could execute doUpdateTimeSlider since there is no code in it to actually update the the timeSlider seekbar.

public class PlayActivity extends AppCompatActivity {

private static final String TAG = "PlayActivity";
boolean playing = false;
private int timeSliderInterval = 1000; // 1 second
private Handler timeSliderHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_play);

    final Runnable doUpdateTimeSlider = new Runnable() {
        @Override
        public void run() {
           timeSliderHandler.postDelayed(updateTimeSlider, timeSliderInterval);
           updateTimeSlider();
        }
    };


回答1:


You could replace:

timeSliderHandler.postDelayed(updateTimeSlider, timeSliderInterval);

with:

timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);

You forgot to write do in doUpdateTimeSlider.




回答2:


As you can see here : https://stackoverflow.com/a/41413191/4409113

It is : doUpdateTimeSlider

final Runnable doUpdateTimeSlider = new Runnable() {
     @Override
     public void run() {
         timeSliderHandler.postDelayed(doUpdateTimeSlider, timeSliderInterval);
         updateTimeSlider();
     }
 };

Which @ρяσѕρєя K said the same.

Update:

To fix that may not have been initialized you can follow these links:

variable r might not have been initialized

"Variable example might not have been initialized" in anonymous class

So:

final Runnable etc = null;
    final Runnable doUpdateTimeSlider = new Runnable() {
        @Override
        public void run() {
            timeSliderHandler.postDelayed(etc, timeSliderInterval);
            updateTimeSlider();
        }
    };

This should work now!



来源:https://stackoverflow.com/questions/41414330/android-java-cannot-resolve-in-postdelayed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!