java Android 如何通过公共静态方法在 TextView 中设置文本?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14991129/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Android How to Set Text in TextView from A Public Static Method?
提问by user1880779
I have this in onCreate :
我在 onCreate 中有这个:
final TextView text1 = (TextView) findViewById(R.id.txtNextAlarm);
And I'm trying to set a text in a method that is in the same class :
我正在尝试在同一类中的方法中设置文本:
public static void NextTxt(){
text1.setText("");
}
But it doesn't recognize the "text1".
但它无法识别“text1”。
回答by Adam Lopresto
The problem is that static methods aren't associated with any particular object, but with the class as a whole. As such, they can only see static fields in your class. Your text1
variable isn't even that, if what you say is true. Instead, it's a local variable that only exists for the length of the onCreate()
method. If you know you'll only ever have one instance of your activity (and that's probably not an unreasonable assumption), what you could do is use
问题在于静态方法不与任何特定对象相关联,而是与整个类相关联。因此,他们只能看到您班级中的静态字段。text1
如果你说的是真的,你的变量甚至不是那个。相反,它是一个只存在于onCreate()
方法长度的局部变量。如果您知道您的活动只会有一个实例(这可能不是一个不合理的假设),您可以做的是使用
private static TextView text1;
at the top of your class (or, basically, anywhere outside of a method). The final
modifier doesn't buy you anything. Your choice of whether to make it public or private, but I tend toward private by default (unless there's a reason for something else).
在班级的顶部(或者,基本上,在方法之外的任何地方)。该final
修改并没有买任何东西。您可以选择将其设为公开还是私有,但默认情况下我倾向于私有(除非有其他原因)。
The alternative is to ask yourself why NextTxt()
is static; if you make it a normal instance method, then you'd still need to declare text1
in the class, but it wouldn't need to be static. But then you'd need an instance to call it on.
另一种方法是问问自己为什么NextTxt()
是静态的;如果你使它成为一个普通的实例方法,那么你仍然需要text1
在类中声明,但它不需要是静态的。但是你需要一个实例来调用它。
回答by Ahmad
TextView text1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text1 = (TextView) findViewById(R.id.txtNextAlarm);
}
Do the initialization in the onCreate method.
在 onCreate 方法中进行初始化。
回答by Krylez
If it's true that this line is in your onCreate method
如果这一行确实在您的 onCreate 方法中
final TextView text1 = (TextView) findViewById(R.id.txtNextAlarm);
then the answer to your question is that text1 is out of scopefrom within your NextTxt method. You've declared and initialized a variable within one method and you're trying to access it from another one. In order for the NextTxt method to "see" text1, you need to move that member to a place where both methods can access it.
那么您的问题的答案是 text1 超出了NextTxt 方法的范围。您已经在一个方法中声明并初始化了一个变量,并且您正试图从另一个方法访问它。为了让 NextTxt 方法“看到”text1,您需要将该成员移动到两个方法都可以访问它的位置。
As mentioned in other answers, you're also dealing with the fact that onCreate is an instance method while NextTxt is a static method. You may be tempted to start making everything static in order to "fix" your issues, but this is a dangerous and sloppy path. You don't have control over when Android kills your UI, so text1 could become invalid with no warning. The next time you try to call a method on it, you won't like the results.
正如其他答案中提到的,您还要处理 onCreate 是一个实例方法而 NextTxt 是一个静态方法的事实。为了“解决”您的问题,您可能想开始让一切都静止不动,但这是一条危险而草率的道路。您无法控制 Android 何时终止您的 UI,因此 text1 可能会在没有警告的情况下变得无效。下次您尝试对其调用方法时,您不会喜欢结果。
Rethink what you're trying to do, sketching it out if necessary and don't just apply quick fixes in Eclipse if you don't understand the error.
重新考虑您正在尝试做什么,如有必要,将其勾勒出来,如果您不理解错误,不要只在 Eclipse 中应用快速修复。
回答by Zielony
If the method is static, you cannot access any of the non-static fields of the class. You have to make your textField static or pass it as a parameter.
如果该方法是静态的,则不能访问该类的任何非静态字段。您必须将 textField 设为静态或将其作为参数传递。
static TextView text1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
MyClass.text1 = (TextView) findViewById(R.id.txtNextAlarm);
}
public static void NextTxt(){
MyClass.text1.setText("");
}
Of course you can only have one textField set at the time, because it's a static field of the class. The other options include making a singleton or removing static modifier from your NextTxt method.
当然你一次只能设置一个textField,因为它是类的静态字段。其他选项包括创建单例或从 NextTxt 方法中删除静态修饰符。
回答by K_Anas
text1 is a local
variable you have to declare it as an attribute of your class
text1 是一个local
变量,您必须将其声明为类的属性
public final TextView text1;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
text1 = (TextView) findViewById(R.id.txtNextAlarm);
}
and in your static method use:
并在您的静态方法中使用:
public static void NextTxt(){
text1.setText("");
}