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.
Calling
findViewById()on theActivityobject will only work if the current Activity layout is set bysetContentView. If you add a layout through some other means, then you need theViewobject of the layout and callfindViewById()on it.If the layout is supposed to be the main layout of the activity, then do this: