不能引用在不同方法中定义的内部类中的非final变量

时间:2012-09-27 08:41:56

标签: java android

  

可能重复:
  Cannot refer to a non-final variable inside an inner class defined in a different method

我的程序中存在一些问题。

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

6 个答案:

答案 0 :(得分:5)

在您的方法之外声明您的字段(在班级)。

答案 1 :(得分:2)

最简单(没有思考)的方法是创建一个重复的,但对同一个对象的final引用:

ShapeDrawable d4 = new ShapeDrawable(new OvalShape());
final ShapeDrawable d4Final = d4;

// you can use d4Final in your inner class

答案 2 :(得分:1)

在你的onCreate()方法之前放

ShapeDrawable d4;

onCreate方法中

d4 = new ShapeDrawable(new OvalShape());

答案 3 :(得分:0)

让你的类实现接口,即:

public class MainActivity extends Activity implements View.OnClickListener {

然后将onClick作为类方法并将其添加到按钮中,如下所示:

btn_start.setOnClickListener(this);

你还必须使d4成为一个实例变量,所以你的类看起来像这样:

public class MainActivity extends Activity implements View.OnClickListener {

ShapeDrawable d4 = new ShapeDrawable(new OvalShape());

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activityenter code here`_main);

    ImageView ob4 = (ImageView) findViewById(R.id.imageView4);

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


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

}

答案 4 :(得分:0)

你可以把它变成最终而不是改变它。最终只表示对象的内存地址相同。

final ShapeDrawable d4;
d4.setShape(new Shape());

没关系;

答案 5 :(得分:0)

试试这个:

public class MainActivity extends Activity {

ImageView ob4;
ShapeDrawable d4;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activityenter code here`_main);

ob4 = (ImageView) findViewById(R.id.imageView4);
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;
}
}