Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Rect set() method missing

I'm using Android Studio 1.1.0 and am trying to use the Rect .set() method in some code to set the edges of a newly-created rectangle, and I'm getting the message "Cannot resolve symbol 'set'" even though I imported android.graphics.Rect as prompted.

When I declare a new Rect and define its edges at the same time, it works fine.

But, if I try to declare a new empty Rect and then define its edges on another line, it's failing to recognize that the set() method exists (or I'm trying to access it incorrectly?).

Also, rather than prompting me with the various Rect functions when I type the Rect name followed by '.', it pops up "ClassLoaderCreator" and "Creator".

What am I doing wrong, and how can I get this and the other Rect functions to work? I tried cleaning and rebuilding my project.

import android.graphics.Rect;

public class MainActivity extends ActionBarActivity {

Rect staticRect = new Rect(100,100,200,200); // works fine

Rect dynamicRect = new Rect(); // this is okay, too, but not useful until set

dynamicRect.set(200,200,300,300); // Cannot resolve symbol 'set'


dynamicRect. // As I start typing here, after the . none of the Rect functions appear, 
             // just "ClassLoaderCreator<T>" and "Creator<T>"

...
like image 730
neongreensticker Avatar asked Apr 01 '26 12:04

neongreensticker


1 Answers

The problem is you're trying to manipulate the instance variable dynamicRect from a static context, i.e. from outside of an instance method. Here are your options:

Create a method to do your initialization, and call it where appropriate. For example:

private void setupRects() {
    dynamicRect.set(200,200,300,300);
    // do other stuff
}

You could also just place the initialization code in your activity's onCreate method if appropriate.

Alternatively, if you wish to initialize dynamicRect statically, you could do the following:

static Rect dynamicRect = new Rect();

static {
    dynamicRect.set(200,200,300,300);
    // do other stuff
}
like image 129
bmat Avatar answered Apr 03 '26 03:04

bmat