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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T07:27:58+00:00 2026-05-18T07:27:58+00:00

I have an app that is setting the contentView when images are clicked.This one

  • 0

I have an app that is setting the contentView when images are clicked.This one from my main.xml loads the options.xml fine.

settings.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {
                 setContentView(R.layout.options);

            }

        });

But when I add a listener to an image that is inside the options.xml the app crashes upon launch.I am referencing the image via the code below and showing the clickListener I am adding.

//ReturnHome is inside options.xml
//Adding this in my mainapp.java
 returnHome = (ImageView)findViewById(R.id.returnHome);
    returnHome.setOnClickListener(new OnClickListener(){

            @Override
            public void onClick(View v) {

            ***APP CRASHES EVERY TIME WHEN I ADD THIS****   
            }

    });

Trying to see if there was an error thrown but LogCat does not seem to show anything.

  • 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-18T07:27:59+00:00Added an answer on May 18, 2026 at 7:27 am

    findViewById for the returnHome image is in context of the application.

    If in your applicaiton onCreate() you set setContentView(R.layout.main); when you call findViewById it will be looking at main.xml for the image returnHome, which obviously isn’t there because its on options.xml, as a result you will get a NullPointerException, because if can’t find what you are asking it to.

    Instead of declaring the image listener for returnHome in the context of main.xml try doing it after changing the layout, maybe as a method:

    private void changeView()
    {
        setContentView(R.layout.options);
        returnHome = (ImageView)findViewById(R.id.returnHome);
        returnHome.setOnClickListener(new OnClickListener()
        {
            @Override
            public void onClick(View v) 
            {
                // Perform whatever.....  
            }
        });
    }
    

    Then change the first onclick listener to:

    settings.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            changeView();
        }
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

This is probably a setting error somewhere. I have a django app that works
I have a rails app that creates a kml file and successfully loads it
I have an app that is highly threaded. The threads in this app read
I have java app that uses Axis to get data from web service. web
I have an app that downloads ZIP files from my server, and can download
I have a CMS app that allows custom fields to be added. One type
I have an app that worked perfectly fine w/ ios 4.x the app works
I have an app that does not currently use a Setting.bundle to display setting
i have an app that has a few checkboxes in the settings and then
I'm writing a simple app that's going to have a tiny form sitting in

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.