34

I have a text view in my Lyout and I would like to set some text to this textview. This should be made in a class which is not a MainActivity class.

The problem is that I got a null pointer exception.

Here is my code:

public class UserInformations extends Activity{

TextView emailTextView;
LocalDatabase localdatabase= new LocalDatabase(this);


    public void getUserInformation()
    {
    emailTextView = (TextView) findViewById(R.id.EmailTextView);
    String email = localdatabase.getUserEmail();
    emailTextView.setText(email);
    }
}

When I am doing this in the Main Activity class, it works, but it doesn't work not in another class.

2
  • the emailTextView = (TextView) findViewById(R.id.EmailTextView); Feb 10, 2012 at 13:53
  • No, my database is in another class. the email value is good, so i think that the problem comes foom the textView Feb 10, 2012 at 13:55

4 Answers 4

47

Calling findViewById() on the Activity object will only work if the current Activity layout is set by setContentView. If you add a layout through some other means, then you need the View object of the layout and call findViewById() on it.

View v = inflater.inflate(id_number_of_layout); # such as R.layout.activity_main
View innerView = v.findViewById(id_number_of_view_inside_v);

If the layout is supposed to be the main layout of the activity, then do this:

public class MyActivity extends Activity{
  TextView emailTextView; 

  @Override
  public void onCreate(Bundle savedInstanceState){
     super.onCreate(savedInstanceState);
     setContentView(id_number_of_layout);
     emailTextView = (TextView) findViewById(R.id.EmailTextView);
     // ... whatever other set up you need to do ...
  }

  public void getUserInformation() {
     // .... regular code ... 
  }
}
2
  • Thank you DeeV, i have some problems to understand this code, sorry. So, in my main activity, I was already calling the setcontentview(...) but now in my second class,how can I find the Id number of my id_number_of_layout and the id_number_of_view_inside_v Feb 10, 2012 at 14:06
  • You know what they are already. In your case, you need to retrieve an instance of the layout that contains the view with the ID R.id.EmailTextView. Generally, if you're using another class outside the Activity class, then you have to pass the view in via constructor or setter method. Since the main view is already inflated in your main activity class, then you probably want to call findViewById(R.id.EmailTextView) in that class, then pass the object reference of the text view to your other class.
    – DeeV
    Feb 10, 2012 at 14:13
6
findViewById(R.id.EmailTextView);

You either

  1. Didn't set the View layout
  2. Have no View with ID EmailTextView, and thus findViewById returns null. You don't have to put the type, but the ID you've given him in the XML.

EDIT: definitely the 1 based on your new comments.

1
  • Thank you, but how to see the view layout? Feb 10, 2012 at 14:07
6

You have not set the content View yet?


to do this use something like this:

public class MainActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    }


}
4
  • No, how to do this, i am working for the first time with content view's. Feb 10, 2012 at 13:54
  • 1
    You need to include this in every Activity. Feb 10, 2012 at 14:06
  • This is not resolwing the problem Feb 10, 2012 at 14:09
  • Well how did you define the EmailTextView? You would usually construct this view inside a layout, when you first create the project you have a file named main.xml in res > layouts > this is a layout which you put other views in, What you need to do is create a new layout, and put this emailtextview in it, then put the name of the layout in setContentView(R.id.<layoutname>); and then your problem should be resolved. Whats happening is that you are trying to get a view that is not associated with the layout you have set
    – FabianCook
    Feb 11, 2012 at 0:45
4

When you call findViewById matters. The layout must have already happened. You must already have set a content view, etc.

There are ways to work around this, as shown in other answers, but they work fundamentally differently from findViewById, and should only be used as a substitute if you understand exactly how they work. Most often it's far more efficient to just wait until after the initial layout has already occurred.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.