简体   繁体   中英

Variable is accessed from within inner class inside another inner class

I'm having an inner class within another inner class, in which I'm trying to use final variable outside both inner classes. Here's the code:

final View v = inflater.inflate(R.layout.fragment_floor_plan, container, false);  //final variable
final Button button0 = v.findViewById(R.id.button21);    
button0.setOnClickListener(new View.OnClickListener() {
           @Override
           public void onClick(View v) {
                   final PhotoView photoView = v.findViewById(R.id.photo_view);   //works fine here
                   photoView.setAlpha(0f);                                  
                   System.out.println(photoView.isZoomable());
                   System.out.println(photoView.VISIBLE);

                   photoView1.animate().alpha(0f).setDuration(250);
                   photoView.animate().alpha(1f).setDuration(250);
                   photoView.bringToFront();    

                   photoView.setOnScaleChangeListener(new OnScaleChangedListener() {
                       @Override
                       public void onScaleChange(float scaleFactor, float focusX, float focusY) {
                           if (photoView.getScale() <= photoView.getMinimumScale() + 0.1f) {
                               LinearLayout linearLayout = v.findViewById(R.id.linearLayout); //doesn't work here
                               linearLayout.bringToFront();
                           } 
                       }
                   }
           }
});

How to get it to work inside OnScaleChangedListener?

Now that I look at it, your problem might be that the View v is being passed as a parameter to the onClick method of the OnClickListener, and the code below might be trying to access instead of your top-level View variable.

My recommendation would be to refactor your variable names so that you are referring to the correct variable. If the passed-in View is the one you actually want to use, it should be declared as final in the method signature:

public void onClick(final View v) {

If the variable in the enclosing scope of the nested anonymous class is declared final or effectively final, you should just be able to access it from within any level of nested inner anonymous classes.

To ensure this I made a little example and tried it out myself:

import java.util.function.*;

public class Main {
  public static void main(String[] args) {
    final int test = 11;
    Runnable runnable = new Runnable() {
      public void run() {
        Runnable runnable = new Runnable() {
          public void run() {
            System.out.println(test);
          }
        };
        runnable.run();
      }
    };
    runnable.run();
  }
}

As you can see the second runnable is nested within another runnable, and running the code accesses the integer test and prints it as it should.

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