可能重复:
无法引用在不同方法中定义的内部类中的非最终变量
我的程序有一些问题。
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityenter code here`_main);
ImageView ob4 = (ImageView) findViewById(R.id.imageView4);
ShapeDrawable d4 = new ShapeDrawable(new OvalShape());
d4.setIntrinsicHeight(150);
d4.setIntrinsicWidth(40);
d4.getPaint().setColor(Color.WHITE);
d4.setVisible(true, true);
ob4.setImageDrawable(d4);
Button btn_start = (Button) this.findViewById(R.id.button1);
btn_start.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//Cannot refer to a non-final variable d4 inside an inner class defined in a different method
//But i don't want to make this field "final", becouse i will change color of this shape in future. Thanks.
d4.getPaint().setColor(Color.RED);
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}