0

After a click on button1, another layout and another class gets called. Now I want to change the text of textView out of class 2 which results in an app crash with java.lang.NullPointerException

important parts of Class 1

public static TextView A;
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

[button stuff in class 1]

 setContentView(R.layout.raten);
                      final    TextView A = (TextView) findViewById(R.id.A); //the textview I wanna chage
                 max = 10;
                      Easy easy = new Easy(); // the other class
                      easy.e();

[now the method in class 1 that should change the text]

    public static void Tx(int i)
    {
 A.setText("adsfasdf");
    }

[important parts of Class 2 ("Easy")]

public void e(){
        System.out.println("called class easy");

    int max = MainActivity.max;
    System.out.println(max);
    for (int i= 0; i<max; i++){
        System.out.println("runde"+i);
 MainActivity.Tx(i);

    }

I know, some people already asked such questions but I didn't find a working solution. I already understood, that you can't access the UI things outside the UI thread and that the nullpointerexception appears, because he uses the "empty" public static TextView A; and not the final TextView A = (TextView) findViewById(R.id.A).

But how I can make it visible for the other methods?

Sorry if the post looks messed up but I didn't konw how to explain my situation in a better way

2 Answers 2

1

Rather than defining a new local variable A, just assign to the static.

change

final    TextView A = (TextView) findViewById(R.id.A); //the textview I wanna chage

to

A = (TextView) findViewById(R.id.A); //the textview I wanna chage
Sign up to request clarification or add additional context in comments.

4 Comments

Thanks a lot for your answer, but unfortunately that didn't solve anything, I get exactly the same error like before :/
Have you verified that the call to findViewById is not returning null?
I already mentioned that, the findviewbyid is not read by Tx(). Tx() uses the public TextView A;thats the problem
I realise that. You have a class variable and a local variable both called A. You assign to the local, so of course the class varibale is still null. When you assignd to the class variable it was still null, so possibly the call to findViewById is returning null. You should try to avoid statics, perhaps you could pass a pointer to the class 1 instance to Easy.e
0

remove static from both A and Tx (of course your MainActivity must be created by the time you call easy.e())

3 Comments

So I did, "Cannot make a static reference to the non-static method Tx(int) from the type MainActivity" .. What do you mean with MainActivity must be created the time I call easy.e() ?
call it this way MainActivity.this.Tx(i);
Sorry for wasting your time, but now I got "No enclosing instance of the type MainActivity is accessible in scope" on that method. Just tried it with making an object of MainActivity, but that results in the same

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.