Not a finite variable inside an inner class

I am trying to dynamically create an ImageView, and I want to pass this ImageView as a method parameter to the listener.

ImageView imageView1 = new ImageView(LookActivity.this); imageView1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { detectLocationAndShowPopUp(imageView1); return true; } }) 

But I accept the following error:
You cannot reference a non-final variable imageView1 inside an inner class defined by another method.

I do not want to declare imageView as final. How can I solve this problem?

+4
source share
6 answers

You can make a copy of imageView1, and then use the copy inside the listener:

 ImageView imageView1 = new ImageView(LookActivity.this); final ImageView imageView2 = imageView1; imageView1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { detectLocationAndShowPopUp(imageView2); return true; } }); 

After Sam's comment, I change my code to:

 ImageView imageView1 = new ImageView(LookActivity.this); imageView1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View view, MotionEvent event) { detectLocationAndShowPopUp((ImageView) view); return true; } }); 
+9
source

Since this is Android, arg0 will be your image when you touch it. Using:

 detectLocationAndShowPopUp((ImageView) arg0); 
+4
source

Create ImageView imageView1 as a global class variable.

And initialize it inside the function, as you do, without declaring it again.

Sort of

 MyClass extends ... { .... ImageView imageView1; . . . . . myFucntion() { imageView1 = new ImageView(LookActivity.this); } } 
+2
source

Do not define an ImageView in a method. Make it a member variable declared under your class definition

+2
source

You are using an anonymous class, not an inner class (for example, the name says). In anonymous classes, you can only refer to final "variables".

If you do not want to add final to the imageView and do not want to use another final variable, you can use the inner class:

 public class YourActivity extends ... { public void yourMethod() { ImageView imageView1 = new ImageView(LookActivity.this); imageView1.setOnTouchListener(new MyListener(imageView1)); } private class MyListener extends OnTouchListener { private ImageView imageView; public MyListener(ImageView iv) { this.imageView = iv; } @Override public boolean onTouch(View arg0, MotionEvent arg1) { detectLocationAndShowPopUp(imageView); return true; } } } 
+2
source

Idea . Use another final variable.

 ImageView imageView1 = new ImageView(LookActivity.this); final ImageView finalImageView = imageView1; imageView1.setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { detectLocationAndShowPopUp(finalImageView); return true; } }) 

Idea two . Use an anonymous subclass of ImageView, then use the ImageView.this link.

 ImageView imageView1 = new ImageView(LookActivity.this) {{ setOnTouchListener(new OnTouchListener() { @Override public boolean onTouch(View arg0, MotionEvent arg1) { detectLocationAndShowPopUp(ImageView.this); return true; } }}; 
+1
source

All Articles