user1880779
user1880779

Reputation: 2078

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

I have this in 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".

Upvotes: 4

Views: 13993

Answers (5)

Krylez
Krylez

Reputation: 17800

If it's true that this line is in your onCreate method

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. 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.

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.

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.

Upvotes: 1

Zielony
Zielony

Reputation: 16537

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.

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.

Upvotes: 1

Ahmad
Ahmad

Reputation: 72563

TextView text1;

@Override
    public void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      text1 = (TextView) findViewById(R.id.txtNextAlarm);
}

Do the initialization in the onCreate method.

Upvotes: 3

Adam Lopresto
Adam Lopresto

Reputation: 346

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

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).

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.

Upvotes: 7

K_Anas
K_Anas

Reputation: 31466

text1 is a local variable you have to declare it as an attribute of your class

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

Upvotes: -2

Related Questions