-4

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

I have some problem in my program.

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;
}
}
Community
  • 1
  • 1
user1702667
  • 11
  • 1
  • 5

6 Answers6

5

Declare your field outside of your method (at class level).

rizzz86
  • 3,862
  • 8
  • 35
  • 52
2

The easiest (no-thinking) way to do this is creating a duplicate, but a final reference to the very same object:

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

// you can use d4Final in your inner class
jabal
  • 11,987
  • 12
  • 51
  • 99
  • btw the reason for you do not want to make `d4` final suggests me that you are not 100% sure what you are doing. Local variables like `d4` have usually a short life-cycle, so probably your intention is not to keep the `d4` reference final. Final makes only the local variable reference final, that let's you change things later IMHO. – jabal Sep 27 '12 at 08:49
  • I think if i make a final variable, a will not change it in future; for exemple final int var=3; i cant do var=5; – user1702667 Sep 27 '12 at 09:17
  • primitives and object references are fundamentally different. there are mutable and immutable objects.. :-) – jabal Sep 27 '12 at 10:06
1

Before your onCreate() method put this

ShapeDrawable d4;

In your onCreate method put

d4 = new ShapeDrawable(new OvalShape());
MGDroid
  • 1,659
  • 2
  • 17
  • 32
  • no,becouse i must create oval before button click – user1702667 Sep 27 '12 at 08:59
  • by this too you are creating it in onCreate() method. You are just giving reference it before onCreate() method. I had similar problem , so I know it well. – MGDroid Sep 27 '12 at 09:03
  • when i press start-color is changed to RED, but then i press STOP Button btn_stop = (Button) this.findViewById(R.id.button2); btn_stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { d4.getPaint().setColor(Color.BLUE); } }); and color doesn't change – user1702667 Sep 27 '12 at 09:15
0

Just make your class implement the interface, ie:

public class MainActivity extends Activity implements View.OnClickListener {

Then have onClick as a class method and add it to your button like this:

btn_start.setOnClickListener(this);

You also have to make d4 an instance variable, so your class would look like this:

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

}

lotophage
  • 163
  • 7
0

You can make it final and than change it. Final only means that memory address of object is same.

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

it will be ok;

Artem Zelinskiy
  • 2,201
  • 16
  • 19
0

Try This :

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;
}
}
Eldhose M Babu
  • 14,382
  • 8
  • 39
  • 44
  • when i press start-color is changed to RED, but then i press STOP Button btn_stop = (Button) this.findViewById(R.id.button2); btn_stop.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { d4.getPaint().setColor(Color.BLUE); } }); and color doesn't change – user1702667 Sep 27 '12 at 09:11