无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误

Posted

技术标签:

【中文标题】无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误【英文标题】:Cannot refer to the non-final local variable button defined in an enclosing scope, Random method error 【发布时间】:2015-11-25 04:03:07 【问题描述】:

我在使用 Eclipse 的项目中遇到错误:

Cannot refer to the non-final local variable button defined in an enclosing scope

这是我的课:

import android.app.Activity;
import android.content.Context;
import android.graphics.Point;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.widget.Button;
import android.view.Menu;
import android.view.MenuItem;
import android.view.WindowManager;

import java.util.Random;
import java.util.Timer;
import java.util.TimerTask;

public class MainActivity extends Activity 
Button buttonblack;
@Override
protected void onCreate(Bundle savedInstanceState) 
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

     buttonblack = (Button)findViewById(R.id.Button01);

     startRandomButton(buttonblack);    



public static Point getDisplaySize(@NonNull Context context) 
    Point point = new Point();
    WindowManager manager = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
    manager.getDefaultDisplay().getSize(point);
    return point;


private void setButtonRandomPosition(Button button)
    int randomX = new Random().nextInt(getDisplaySize(this).x);
    int randomY = new Random().nextInt(getDisplaySize(this).y);

    button.setX(randomX);
    button.setY(randomY);


private void startRandomButton(Button button) 
    Timer timer = new Timer();
    timer.schedule(new TimerTask() 
        @Override
        public void run() 
            setButtonRandomPosition(button);
        
    , 0, 1000);//Update button every second

@Override
public boolean onCreateOptionsMenu(Menu menu) 
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;


@Override
public boolean onOptionsItemSelected(MenuItem item) 
    // Handle action bar item clicks here. The action bar will
    // automatically handle clicks on the Home/Up button, so long
    // as you specify a parent activity in AndroidManifest.xml.
    int id = item.getItemId();
    if (id == R.id.action_settings) 
        return true;
    
    return super.onOptionsItemSelected(item);


问题出在这个方法上:

private void startRandomButton(Button button) 
        Timer timer = new Timer();
        timer.schedule(new TimerTask() 
            @Override
            public void run() 
                setButtonRandomPosition(button);// Cannot refer to the non-final local variable button defined in an enclosing scope
            
        , 0, 1000);//Update button every second
    

有人可以帮我解决这个问题吗?或者建议任何其他可能比这更好的代码?

【问题讨论】:

【参考方案1】:

将您的代码更改为private void startRandomButton(final Button button)。编译器希望确保在匿名类的方法中没有重新分配引用。

从 java-8 开始,如果您的引用是有效最终,那么您甚至不必将这些参数标记为 final

【讨论】:

【参考方案2】:

您已在MainActivity..中全局声明Button buttonblack;

您可以像这样setButtonRandomPosition(buttonblack); 传递buttonblack 而无需将其称为final

【讨论】:

感谢 uday 真的很有帮助!但是应用程序被强制关闭,你能再帮我一次吗?在我的新帖子***.com/questions/32307525/…

以上是关于无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误的主要内容,如果未能解决你的问题,请参考以下文章

无法分配最终的局部变量“name”,因为它是在封闭类型中定义的

Java停止了内部类中非最终变量的错误(java 8)[重复]

JS封闭函数闭包内置对象

为啥尚未定义的局部范围变量引用同名的实例变量?

“有效最终”的变量是啥意思? [复制]

Lambda 表达式和变量捕获