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 3855746
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 19, 20262026-05-19T17:49:24+00:00 2026-05-19T17:49:24+00:00

I’m having trouble adding a button to a layout that I’ve created in XML.

  • 0

I’m having trouble adding a button to a layout that I’ve created in XML. Here’s what I want to achieve:

//some class
else {
        startActivity(new Intent(StatisticsScreen.this, ScreenTemperature.class));
}
////

//ScreenTemperatureClass
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    //this is where I call another class that
    //displays a nice graph
    setContentView(new GraphTemperature(getApplicationContext()));

}

I want to add a Button to this new screen so that it’ll appear below the graph.
I’ve tried creating a LinearLayout view, then create a Button and add it to this view but I just get NullPointerExceptions..

Any help would be appreciated. Thanks

EDIT#1

Here’s what I’ve tried using that created a NullPointerException and ‘force close’:

Button buybutton;
LinearLayout layout;

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

    setContentView(new GraphTemperature(getApplicationContext()));

    layout = (LinearLayout) findViewById(R.id.statsviewlayout);
    Button buyButton = new Button(this);
    buyButton.setText(R.string.button_back);
    buyButton.setLayoutParams(new LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    layout.addView(buyButton);

}

And here’s the logcat error:

ERROR/AndroidRuntime(293): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.weatherapp/com.weatherapp.ScreenTemperature}: java.lang.NullPointerException
ERROR/AndroidRuntime(293):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2663)
ERROR/AndroidRuntime(293):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
ERROR/AndroidRuntime(293):     at android.app.ActivityThread.access$2300(ActivityThread.java:125)
ERROR/AndroidRuntime(293):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)

theres abviously more lines to do with this error in logcat, not sure if you want it?

EDIT#2

So i tried bhups method:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    GraphTemperature GT = new GraphTemperature(getApplicationContext());             
    layout = (LinearLayout) findViewById(R.id.statsviewlayout);
    Button buyButton = new Button(this);
    buyButton.setText(R.string.button_back);
    buyButton.setLayoutParams(new LayoutParams(
        ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    layout.addView(GT); // line 27
    layout.addView(buyButton);       
    setContentView(layout);           
}

This method produced the same logcat error as above, NullPointerException, indicating it was something to do with line no. 27 which is the layout.addView line of code. Any ideas? Thanks again

  • 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-19T17:49:25+00:00Added an answer on May 19, 2026 at 5:49 pm

    This line:

    layout = (LinearLayout) findViewById(R.id.statsviewlayout);
    

    Looks for the “statsviewlayout” id in your current ‘contentview’. Now you’ve set that here:

    setContentView(new GraphTemperature(getApplicationContext()));
    

    And i’m guessing that new “graphTemperature” does not set anything with that id.

    It’s a common mistake to think you can just find any view with findViewById. You can only find a view that is in the XML (or appointed by code and given an id).

    The nullpointer will be thrown because the layout you’re looking for isn’t found, so

    layout.addView(buyButton);
    

    Throws that exception.

    addition:
    Now if you want to get that view from an XML, you should use an inflater:

    layout = (LinearLayout) View.inflate(this, R.layout.yourXMLYouWantToLoad, null);
    

    assuming that you have your linearlayout in a file called “yourXMLYouWantToLoad.xml”

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We're building an app, our first using Rails 3, and we're having to build
I have some data like this: 1 2 3 4 5 9 2 6
I have a bunch of posts stored in text files formatted in yaml/textile (from
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I am trying to loop through a bunch of documents I have to put
I'm making a simple page using Google Maps API 3. My first. One marker

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.