【问题标题】:non-final variable inside an inner class内部类中的非最终变量
【发布时间】:2013-02-22 15:58:47
【问题描述】:

我尝试动态创建一个 ImageView,我想将此 imageView 作为参数传递给侦听器的方法。

            ImageView imageView1 = new ImageView(LookActivity.this);

            imageView1.setOnTouchListener(new OnTouchListener() {

                    @Override
                    public boolean onTouch(View arg0, MotionEvent arg1) {
                        detectLocationAndShowPopUp(imageView1);
                        return true;
                    }
                })

但我遇到以下错误:
不能在以不同方法定义的内部类中引用非最终变量 imageView1。

我不想将 imageView 声明为 final。我该如何克服这个问题?

【问题讨论】:

标签: java android


【解决方案1】:

您可以制作 imageView1 的副本,然后在侦听器中使用该副本:

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;
    }
});

在 Sam 发表评论后,我将代码更改为:

ImageView imageView1 = new ImageView(LookActivity.this);

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

【讨论】:

  • 这是一个很好的通用答案。但是对于那些不了解 Android 的人来说,arg0 是对 imageView1 的引用,如果需要,只需将其转换为 ImageView。
【解决方案2】:

由于这是 Android,当您触摸它时,arg0 将成为您的图像。使用:

detectLocationAndShowPopUp((ImageView) arg0);

【讨论】:

  • 欢迎来到 StackOverflow!通过添加代码示例和使用标记工具,我稍微改进了您的答案。我也给了你一个赞成票,因为这个答案是正确的。您可以通过单击“edit”来使用How to Ask 中的一些建议再次改进您的答案。
  • 哎呀!我的意思是喜欢How to Answer... :)
【解决方案3】:

创建ImageView imageView1 作为全局类变量。

并在函数内部初始化它,无需再次声明。

类似

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

}

【讨论】:

  • 你抢我的例子,是的,应该这样做
【解决方案4】:

不要在方法中定义ImageView。使其成为在您的类定义下声明的成员变量

【讨论】:

    【解决方案5】:

    您使用的是匿名类而不是内部类(如标题所示)。在匿名类中只能引用final“变量”。

    如果您不想将final 添加到imageView 并且不想使用另一个最终变量,您可以使用内部类:

    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;
        }
      }
    }
    

    【讨论】:

      【解决方案6】:

      想法一:使用另一个最终变量。

      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;
        }
      })
      

      想法二:使用 ImageView 的匿名子类,然后使用 ImageView.this 引用。

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

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-08-13
        • 1970-01-01
        相关资源
        最近更新 更多