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?
Change your code to
private void startRandomButton(final Button button)
. The compiler wants to make sure that a reference is not being re-assigned inside a method of an anonymous class.From java-8, If your reference is effectively-final, then you don't even have to mark those arguments as final
You have declared
Button buttonblack;
Globally inMainActivity
..you can pass
buttonblack
like thissetButtonRandomPosition(buttonblack);
without referring it asfinal
.