简体   繁体   English

缺少Android Rect set()方法

[英]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. 我正在使用Android Studio 1.1.0并尝试在某些代码中使用Rect .set()方法来设置新创建的矩形的边缘,并且我收到消息“无法解析符号'set'”即使我按照提示导入了android.graphics.Rect。

When I declare a new Rect and define its edges at the same time, it works fine. 当我声明一个新的Rect并同时定义它的边时,它工作正常。

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?). 但是,如果我尝试声明一个新的空Rect然后在另一行上定义它的边缘,它就无法识别set()方法是否存在(或者我试图错误地访问它?)。

Also, rather than prompting me with the various Rect functions when I type the Rect name followed by '.', it pops up "ClassLoaderCreator" and "Creator". 另外,当我输入Rect名称后跟'。'时,不是提示我使用各种Rect函数,而是弹出“ClassLoaderCreator”和“Creator”。

What am I doing wrong, and how can I get this and the other Rect functions to work? 我做错了什么,如何让这个和其他Rect函数起作用? 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>"

...

The problem is you're trying to manipulate the instance variable dynamicRect from a static context, ie from outside of an instance method. 问题是你试图从静态上下文操作实例变量dynamicRect ,即从实例方法的外部操作。 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. 如果合适,您也可以将初始化代码放在activity的onCreate方法中。

Alternatively, if you wish to initialize dynamicRect statically, you could do the following: 或者,如果您希望静态初始化dynamicRect ,可以执行以下操作:

static Rect dynamicRect = new Rect();

static {
    dynamicRect.set(200,200,300,300);
    // do other stuff
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM