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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:03:57+00:00 2026-05-23T13:03:57+00:00

I’ve got 2 layout files in res/layout folder: main.xml and page2.xml. In the main.xml

  • 0

I’ve got 2 layout files in res/layout folder: main.xml and page2.xml.
In the main.xml I’ve got welcome info and button which starts

setContentView(R.layout.page2);

to change to page2.xml.

It worked fine till I decided to add Gallery view in page2.xml.

When I set from begining ContentView to page2 like below it’s ok.

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.page2); 
    Gallery g = (Gallery) findViewById(R.id.gallery);       
    g.setAdapter(new ImageAdapter(this));

But when I call main.xml first to show may start page…

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main); 
    Gallery g = (Gallery) findViewById(R.id.gallery);       
    g.setAdapter(new ImageAdapter(this));

the app returns error.
I know that the problem is with Context in line

    g.setAdapter(new ImageAdapter(this));

but I completely don’t know how to pass right context or solve it in another way (but I don’t want to have all layout in one xml file).

  • 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-23T13:03:57+00:00Added an answer on May 23, 2026 at 1:03 pm

    It’s not entirely clear from your explanation (logs never hurt), but I think you’re getting a null pointer exception because gallery is not defined in your main.xml. There are two solutions for your problem:

    1. Split your two “pages” into two activities. This is a much more natural way of dealing with things. Try rotating your phone/emulator and you’ll see what I mean. When the user clicks on the button or whatever, call startActivity() and then call finish(), so your welcome activity is not left hanging about.
    2. The problem is that findViewById() acts on whatever is “visible” in the activity right now. Since you did setContentView(main), your Gallery will not be there. Try “getting” the gallery only after you make the call to “change pages” (setContentView(R.layout.page2);).

    However, I would strongly advice you go with the first option.

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

Sidebar

Related Questions

link Im having trouble converting the html entites into html characters, (&# 8217;) i
I am trying to understand how to use SyndicationItem to display feed which is
I used javascript for loading a picture on my website depending on which small
I've got a string that has curly quotes in it. I'd like to replace
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have a text area in my form which accepts all possible characters from
i got an object with contents of html markup in it, for example: string
I have thousands of HTML files to process using Groovy/Java and I need to
I'm parsing an XML file, the creators of it stuck in a bunch social

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.