简体   繁体   English

Android如何从公共静态方法在TextView中设置文本?

[英]Android How to Set Text in TextView from A Public Static Method?

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”。

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. 你的text1变量甚至不是,如果你说的是真的。 Instead, it's a local variable that only exists for the length of the onCreate() method. 相反,它是一个局部变量,只存在于onCreate()方法的长度。 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 如果你知道你只有一个你的活动实例(这可能不是一个不合理的假设),你可以做的就是使用

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. final修饰符不会给你任何东西。 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). 您选择是公开还是私人,但我默认倾向于私人(除非有其他原因)。

The alternative is to ask yourself why NextTxt() is static; 另一种方法是问问自己为什么NextTxt()是静态的; 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. 如果你使它成为普通的实例方法,那么你仍然需要在类中声明text1 ,但它不需要是静态的。 But then you'd need an instance to call it on. 但是你需要一个实例来调用它。

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方法中进行初始化。

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 scope from within your NextTxt method. 那么您的问题的答案是text1超出了NextTxt方法的范围 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. 为了使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. 正如其他答案中所提到的,您还要处理的事实是onCreate是一个实例方法,而NextTxt是一个静态方法。 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. 您无法控制Android何时杀死您的UI,因此text1可能会变为无效而没有任何警告。 The next time you try to call a method on it, you won't like the results. 下次尝试在其上调用方法时,您将不喜欢结果。

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中应用快速修复。

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. 当然,您当时只能设置一个textField,因为它是类的静态字段。 The other options include making a singleton or removing static modifier from your NextTxt method. 其他选项包括从NextTxt方法创建单例或删除静态修改器。

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("");
}

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

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