无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误 [英] Cannot refer to the non-final local variable button defined in an enclosing scope, Random method error

查看:77
本文介绍了无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的eclipse项目出现错误:

 无法引用封闭范围中定义的非最终局部变量按钮 

这是我的课程:

  import android.app.Activity;导入android.content.Context;导入android.graphics.Point;导入android.os.Bundle;导入android.support.annotation.NonNull;导入android.widget.Button;导入android.view.Menu;导入android.view.MenuItem;导入android.view.WindowManager;导入java.util.Random;导入java.util.Timer;导入java.util.TimerTask;公共类MainActivity扩展了Activity {按钮buttonblack;@Override受保护的void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);buttonblack =(Button)findViewById(R.id.Button01);startRandomButton(buttonblack);}公共静态Point getDisplaySize(@NonNull上下文上下文){点point = new Point();WindowManager管理器=(WindowManager)context.getSystemService(Context.WINDOW_SERVICE);manager.getDefaultDisplay().getSize(point);返回点}私人void setButtonRandomPosition(Button button){int randomX =新的Random().nextInt(getDisplaySize(this).x);int randomY =新的Random().nextInt(getDisplaySize(this).y);button.setX(randomX);button.setY(randomY);}private void startRandomButton(Button button){计时器计时器=新的计时器();timer.schedule(new TimerTask(){@Override公共无效run(){setButtonRandomPosition(button);}},0,1000);//每秒更新按钮}@Overridepublic boolean onCreateOptionsMenu(Menu menu){//膨胀菜单;这会将项目添加到操作栏(如果有).getMenuInflater().inflate(R.menu.main,menu);返回true;}@Overridepublic boolean onOptionsItemSelected(MenuItem item){//单击操作栏项.动作栏将//自动处理主页"/向上"按钮上的点击,//当您在AndroidManifest.xml中指定父活动时.int id = item.getItemId();如果(id == R.id.action_settings){返回true;}返回super.onOptionsItemSelected(item);}} 

问题出在这种方法中:

  private void startRandomButton(按钮按钮){计时器计时器=新的计时器();timer.schedule(new TimerTask(){@Override公共无效run(){setButtonRandomPosition(button);//无法引用封闭范围中定义的非最终局部变量按钮}},0,1000);//每秒更新按钮} 

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

解决方案

您已在 MainActivity 中全局定义了 Button buttonblack; .

您可以像这样 setButtonRandomPosition(buttonblack); 这样传递 buttonblack ,而不必将其称为 final .

I am getting an error in my project with eclipse:

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

This my class:

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

The problem is in this method:

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
    }

Can someone help me fix this? Or suggest any other code that could be better than this?

解决方案

You have declared Button buttonblack; Globally in MainActivity..

you can pass buttonblack like this setButtonRandomPosition(buttonblack); without referring it as final.

这篇关于无法引用封闭范围中定义的非最终局部变量按钮,随机方法错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆