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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T03:03:41+00:00 2026-06-13T03:03:41+00:00

Short Story: I have a layout layout.xml, which gets replaced by another layout success.xml

  • 0

Short Story:
I have a layout “layout.xml”, which gets replaced by another layout “success.xml” after a successful web request. Both layouts have an ImageView that provides the backgrounds to the layouts. These 2 backgrounds both need to be the same, and both are dependent on a user preference.

Longer Story: This all happens in a Fragmnet with an AsyncTask replacing the contentView with “success.xml” in onPostExecute after the web request. This happens as follows:

View view = getView();
view = null;
view = View.inflate(context, R.layout.success, null);

What I tried to do is give both ImageViews the following android:id="@+id/background_image" and then call

ImageView background = (ImageView)view.findViewById(R.id.background_image);
background.setImageResource(R.drawable.bg1);

This background-setting works for the initial view (layout.xml), but on trying to change to “success.xml”, I get a NullPointException because background is null.

I’ve checked and the View’s id is set to -1 while the original view’s background_image id is set to something sensible and valid.

I’ve also tried setting the second view’s background id like this: android:id="@id/background_image", i.e. without the ‘+’, but still no luck.

The added complication is that it’s not just 2 layouts, but about 5 that I need to do this for, so it would be really handy to recycle view id’s.

Any help would be greatly appreciated.

  • 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-06-13T03:03:42+00:00Added an answer on June 13, 2026 at 3:03 am

    Your code for replacing the fragment’s view will not do what you want, the original view will remain the same as you change only a reference to that view and not the actual object.

    To replace the view of the fragment with the new layout you could have another ViewGroup(for example a FrameLayout) in the basic layout (layout.xml) wrapping your current content(don’t forget to give it an id) of layouts.xml(as I understand this is the basic layout). Then, when it’s time to replace the layout you could simply do:

    // remove the current content
    ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)).removeAllViews();
    // add the new content
    View.inflate(context, R.layout.success, ((ViewGroup) getView().findViewById(R.id.yourWrapperLayout)));
    

    You could avoid adding an extra layout if, by any chance, all your five layouts have the same type for the root view(like a LinearLayout etc). In this case you would use the same code as above but you’ll modify the other layouts file to use a merge tag. Also, you’ll be looking for the id of the root in the layout.xml layout into which you’ll add the content of the other files.

    Then you could have the same ids, but you’ll have to reinitialize any reference to the views(meaning that you’ll have to search for the view again if you store a reference to the view(like a Button field in the fragment class)).

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

Sidebar

Related Questions

So long story short I have an object model in which several different entity
Long story short, I have an ASP.NET application I'm trying to debug and at
So long story short, i have some forms (2) of them that, need different
Short story: stopPropagation() prevents a dropdown menu from closing - which is good. But
To cut a long story short I have a section of my application that
Hey, long story short I have inherited some terrible code. As a result a
Possible Duplicate: Qt equivalent of PathAppend? Short story: does Qt 4 have an analog
The short story is I have a SQL Server DB with varchar fields instead
Story short I have widgets sidebar. I style it like this: .widgets ul {padding:
Long story short: I have 2 collections of objects. One contains good values (Let's

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.