简体   繁体   English

如何访问方法Java之外的局部变量?

[英]How to access local variable outside method Java?

I need to access string stream1 inside method, but its only works on method scope.我需要在方法内部访问字符串 stream1,但它仅适用于方法范围。 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. onTaskCompleted()是一个回调方法。 The Toast inside that method doesn't get called until the asynchronous task is completed.在异步任务完成之前,不会调用该方法中的 Toast。 But the Toast at the bottom of onCreate() gets called right away, without waiting for onTaskCompleted() to finish.但是onCreate()底部的 Toast 会立即被调用,无需等待onTaskCompleted()完成。 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() .所以如果你想设置stream1的值,你必须从onTaskCompleted() And if you want to use the value of stream1 later on, you'll have to somehow check that onTaskCompleted() has executed.如果您想稍后使用stream1的值,您必须以某种方式检查onTaskCompleted()是否已执行。 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.这可以通过直接从onTaskCompleted()调用使用stream1的方法,也可以通过在使用stream1之前对stream1执行空检查来实现。

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.具体来说,在第二个 toast 中,您在调用onTaskCompleted回调之前访问变量。

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.基本上,您的onCreate方法将无法使用由onTaskCompleted设置的变量,因为后者直到onCreate调用返回后才会被调用。

Note that new LowCostVideo.OnTaskCompleted(){...} is actually creating an instance of an anonymous inner class that gets passed to the LowCostVideo instance.请注意, new LowCostVideo.OnTaskCompleted(){...}实际上正在创建一个匿名内部类的实例,该实例被传递给LowCostVideo实例。 That instance's method only gets called when the LowCostVideo object completes a task.该实例的方法仅在LowCostVideo对象完成任务时被调用。

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.在“真正的”代码中,您将不得不弄清楚如何做……无论您真正想做的是什么……以另一种方式。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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