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

标签 java android eclipse button random

我的 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
    }

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

最佳答案

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

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

关于java - 无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32305839/

相关文章:

java 正则表达式 : finding a pattern of one character s follows by 5 digits range 0-9

java - 将 PropertyChangeListener 添加到 JComboBox

android - RecyclerView 未显示在协调器布局中

java - Maven 缺少类

c - 在Eclipse中调试C程序找不到命令行参数文件

Java:在每个数组值前面输入一个数字

java - 从运行时列表中删除重复的字符串

安卓 2.2 到 1.6?

java - 在android中的选项卡小部件和 fragment 上创建未读消息计数

java - 安卓项目: Conversion to Dalvik format failed with error 1