سؤال

In my app in try to change a TextView text using setText. If the call is made in the onCreate method it works just fine but if I call another method to do so it crashes giving me a NullPointerException

here is the working code

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final TextView ca00 = (TextView) findViewById(R.id.case00); 
    ca00.setText("stuff");
}

what I want to do is to have a method that will change my texts whenever I call it so I tried this :

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    final TextView ca00 = (TextView) findViewById(R.id.case00); 
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }

What do I do wrong or how can I manage to do what I want ?

Thanks for your time and answers !

هل كانت مفيدة؟

المحلول

Your problem is variable scope. Inside the onCreate() method you are actually defining a new local variable and not using the class property.

You need to change this line in your onCreate()

final TextView ca00 = (TextView) findViewById(R.id.case00);

To this:

ca00 = (TextView) findViewById(R.id.case00);

This will resolve your NPE.

نصائح أخرى

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

     ca00 = (TextView) findViewById(R.id.case00); 
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }

You are creating TextView ca00 two times in your code, instead that declare it globally and use it directly in onCreate()

package staub.olivier.aros.test;

import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;


public class MainActivity extends Activity {

    public TextView ca00;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ca00 = (TextView) findViewById(R.id.case00);    // Change here
    updateText();
}

    public updateText() {
            ca00.setText("stuff");

    }
    }

your global ca00 is never initialized, hence null pointer exception you experiencing

It's about the problem of scope in your code. In the onCreate method, you declared a ca00 variable:

final TextView ca00 = (TextView) findViewById(R.id.case00); 

Therefore, anything you set to ca00 only affect a variable in that method, not the one in the class scope. You should change to:

this.ca00 = (TextView) findViewById(R.id.case00);  
// this keyword is not mandatory, but I recommend it.
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top