Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7491061
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T15:51:52+00:00 2026-05-29T15:51:52+00:00

I have a text view in my Lyout and I would like to set

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-29T15:51:54+00:00Added an answer on May 29, 2026 at 3:51 pm

    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 ... 
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this nice dialog view I set my UserInputDialog class to: <LinearLayout android:id=@+id/LinearLayout01
I have Tab Layout that contains a TextView. I'd like to assign some text
I have this layout that works correctly, a relative layout with a text view
In my application,i have text view with long text. i need text wrapping like
I have an Android Spinner view in my layout. I would like that spinner
In my main activity, I would like to have it set up, so that
I have a popup text view with a next button and onClick I would
http://dl.dropbox.com/u/24856/android_issue.PNG i have a text view on top running into the relative layout with
I have the following TextView in my XML layout file:- <TextView android:layout_width=fill_parent android:layout_height=wrap_content android:text=@string/autolink_test
I have a custom baseadapter which does some lazy loading of some images, and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.