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

Cannot refer to a non-final variable inside an inner class defined in a different method

本问题已经有最佳答案,请猛点这里访问。

Possible Duplicate:
Cannot refer to a non-final variable inside an inner class defined in a different method

我的程序有点问题。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}
}


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


最简单(无需思考)的方法是创建一个副本,但final引用的是同一个对象:

1
2
3
4
ShapeDrawable d4 = new ShapeDrawable(new OvalShape());
final ShapeDrawable d4Final = d4;

// you can use d4Final in your inner class


在你的方法之前

1
ShapeDrawable d4;

在你的onCreate方法中

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


试试这个:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
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;
}
}


你可以做最后的决定,而不是改变它。最后只表示对象的内存地址相同。

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

一切都会好起来的;


让你的类实现接口,即:

1
public class MainActivity extends Activity implements View.OnClickListener {

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

1
btn_start.setOnClickListener(this);

您还必须使d4成为一个实例变量,因此您的类如下所示:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
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;
}

}