简体   繁体   中英

How to access local variable outside method Java?

I need to access string stream1 inside method, but its only works on method scope. I already declare its as global variable in class. Please see sample code below :

public class EmbeddedPlayerActivity extends AppCompatActivity {
    String stream1;
    LowCostVideo xGetter;
    
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_embedded_player);
        xGetter = new LowCostVideo(this);
        LowCostVideo xGetter = new LowCostVideo(this);
        xGetter.onFinish(new LowCostVideo.OnTaskCompleted() {
    
            @Override
            public void onTaskCompleted(ArrayList<XModel> vidURL, 
                                        boolean multiple_quality) {
                stream1 = "show me";
                Toast.makeText(getApplicationContext(),
                               stream1,Toast.LENGTH_LONG)
                     .show();  // Its works showing text "show me"
            }
        });
        Toast.makeText(getApplicationContext(),
                       stream1,Toast.LENGTH_LONG)
             .show(); // Not works, its give NULL result
    }
}

onTaskCompleted() is a callback method. The Toast inside that method doesn't get called until the asynchronous task is completed. But the Toast at the bottom of onCreate() gets called right away, without waiting for onTaskCompleted() to finish. Simply defining a callback method doesn't mean the code will pause right there and wait for it to complete.

So if you want to set the value of stream1 , you have to do it from onTaskCompleted() . And if you want to use the value of stream1 later on, you'll have to somehow check that onTaskCompleted() has executed. This could by by calling the method that uses stream1 directly from onTaskCompleted() , or it could be by performing a null check on stream1 before using it.

Asynchronous operations are one of the most difficult subjects in programming. There are a million ways to handle it.

The problem is not how you are accessing the variable. The problem when you are accessing it. Specifically, in the second toast you are accessing the variable before the onTaskCompleted callback has been called.

Basically, your onCreate method won't be able to make use of variables set by onTaskCompleted because the latter isn't being called until after the onCreate call has returned.

Note that new LowCostVideo.OnTaskCompleted(){...} is actually creating an instance of an anonymous inner class that gets passed to the LowCostVideo instance. That instance's method only gets called when the LowCostVideo object completes a task.

Clearly what you are doing here is just a test. In the "real" code you are going to have to figure out how to do ... whatever it is you are really trying to do ... in another way.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM