-4

可能重复:
无法引用在不同方法中定义的内部类中的非最终变量

我的程序有一些问题。

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

6 回答 6

5

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

于 2012-09-27T08:44:44.237 回答
2

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

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

// you can use d4Final in your inner class
于 2012-09-27T08:45:41.097 回答
1

在你的onCreate()方法放这个之前

ShapeDrawable d4;

在你的onCreate方法中

d4 = new ShapeDrawable(new OvalShape());
于 2012-09-27T08:50:13.500 回答
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;
}
}
于 2012-09-27T08:53:18.697 回答
0

你可以让它成为最终的,而不是改变它。final 仅表示对象的内存地址相同。

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

会没事的;

于 2012-09-27T08:46:57.463 回答
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;
}

}

于 2012-09-27T08:46:29.117 回答