简体   繁体   中英

How to change text of Text View outside of onCreate()

I am trying to change the text of a TextView on my Activity. It happens in a method in the same class as the onCreate method. However, it does not work. I've googled it but found nothing.

This is the error I got:

Attempt to invoke virtual method 'android.content.pm.ApplicationInfo android.content.Context.getApplicationInfo()' on a null object reference.

This is the Main Activity, where my TextView is located:

    private static TextView welcome;
    private static TextView counter;
    private static int number;
    private static SharedPreferences sharedPreferences;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Shared Preferences getting called to see if the user has set his name
        SharedPreferences sharedpreferences = getSharedPreferences("name", Context.MODE_PRIVATE);

        //Getting number
        this.number = sharedpreferences.getInt("number", 0);
        this.sharedPreferences = sharedpreferences;

        Boolean continueName = false;
        //Checking if name is set
        if (!sharedpreferences.getBoolean("nameSet", false)) {
            //If name is not set
            Intent intent = new Intent(this, Name.class);
            startActivity(intent);
        } else {
            //if name was already set, default activity pops Up
            continueName = true;
        }

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //getting counter and setting it
        TextView count = (TextView) findViewById(R.id.txtNumber);
        this.counter = count;

        //Actionlistener to +1 Button
        ActionListeners al = new ActionListeners();
        Button plusOne = (Button) findViewById(R.id.btnAddOne);
        plusOne.setOnClickListener(al.getPlusOneListener());

        //Setting Text View Object
        this.welcome = (TextView) findViewById(R.id.welcomeUser);

        //If the name is set
        if (continueName) {
            this.welcome.setText(getString(R.string.welcome) + " " + sharedpreferences.getString("usersName", ""));
        }
    }

    public void setNameNew() throws InterruptedException {
        TextView welcomeThis = this.welcome;
        new Thread(new Runnable() {
            public void run() {
                //Setting welcome text
                SharedPreferences sp = getSharedPreferences("name", Context.MODE_PRIVATE);
                welcomeThis.setText(getString(R.string.welcome) + " " + sp.getString("usersName", ""));
            }
        });
        Thread.sleep(500);
        this.welcome.setText(welcomeThis.getText());
    }

        public void changeViewNumber(int number) {
        //Setting new number
        this.counter.setText(number);
    }
}

Weird is, that the setNameNew method is working and can change the text of the TextField . But the changeViewNumber method is not working.

Im on the activity where the TextView is located. I can't figure it out. May you please help me?

You can call setText for a TextView anywhere in the class as long as the TextView is referenced. When you say one function is working and the other isn't, it's because the reference for the counter is incorrect.

Your counter reference

TextView count = (TextView) findViewById(R.id.txtNumber);
this.counter = count;

Your welcome reference

 this.welcome = (TextView) findViewById(R.id.welcomeUser);

You should be referencing the view the way you referenced welcome . You'll want to change the counter reference to the following.

this.counter = (TextView)findViewById(R.id.txtNumber)

Your count variable does nothing and should be removed.

I would also like to make additional notes for your code. You are using the keyword this , it isn't necessary for your code.

The this keyword refers to the current object in a method or constructor.

The most common use of the this keyword is to eliminate the confusion between class attributes and parameters with the same name (because a class attribute is shadowed by a method or constructor parameter.

I would also strongly recommend not putting the main thread to sleep, if the thread is sleeping and action is required it will cause your app to crash.

This is useless

TextView count = (TextView) findViewById(R.id.txtNumber);
this.counter = count;

Just do

counter = findViewById(R.id.txtNumber);

This is because you've declared welcome variable globally in class and initialized in onCreate but you forget to initialize counter on the OnCreate method which is why it is throwing a null pointer exception.

this.counter = (TextView) findViewById(R.id.txtNumber);

just initialize your counter variable just like you did on your welcome variable.

The problem was, that I tried to set the text of a TextView with an Integer The fix was:

String numberString = Integer.toString(number);
counter.setText(numberString);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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