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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T04:17:49+00:00 2026-06-01T04:17:49+00:00

I’m trying to build a basic ListFragment based application which transitions from one ListFragment

  • 0

I’m trying to build a basic ListFragment based application which transitions from one ListFragment to another, based upon user input.

I make use of the default ListView that the Android system inflates for a ListFragment, thus I dont over-ride onCreateView().

To set the margins around the ListFragment, I add a GlobalLayoutListener.

Now, when I launch the application, the first screen containing the default fragment shows up properly, with the margins set correctly.

But as soon as I click an image in the main layout, which invokes a transition to a second fragment having the same onActivityCreated() method and the GlobalLayoutListener as the first fragment, I get the dreaded Content View not created yet error in the OnGlobalLayout() method when I try to access the ListView in the second fragment.

public void onActivityCreated(Bundle savedInstanceState){

     super.onActivityCreated(savedInstanceState);



     setListAdapter(new ArrayAdapter<String>(getActivity(),
                    R.layout.listcommon, R.id.label, MAIN_TITLES));




     ListView lv = getListView();

     lv.setDivider(null);
     lv.setDividerHeight(0);
     lv.setChoiceMode(ListView.CHOICE_MODE_SINGLE);
     lv.setItemChecked(0, true);
     lv.setSelection(0);



     ViewTreeObserver observer = getListView().getViewTreeObserver();

     observer.addOnGlobalLayoutListener(fragmentLayoutListener);

}

ViewTreeObserver.OnGlobalLayoutListener fragmentLayoutListener = new   ViewTreeObserver.OnGlobalLayoutListener(){

    public void onGlobalLayout() {

    //Crashes here for second fragment   
    ListView listView = getListView();

    FrameLayout.LayoutParams params = (LayoutParams) listView.getLayoutParams();

    params.setMargins(10, 10, 10, 10);

    }

};

Here’s the main layout: (Default fragment gets added through FragmentTransaction.add() to first FrameLayout)

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent" 
android:layout_height="match_parent"
android:baselineAligned="false"
android:background="#00000000">


<FrameLayout
    android:id="@+id/TitlesContainer"
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="40"
    android:layout_margin="10dip"
    android:background="@drawable/titlesbg"
    >


</FrameLayout>


<FrameLayout
    android:layout_width="match_parent"
    android:layout_height="0px"
    android:layout_weight="60"
    android:background="#00000000"
    >

<ImageView
    android:id="@+id/imageView_clickWheel"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="center"
    android:src="@drawable/clickImage" />

</FrameLayout>


</LinearLayout>

Any thoughts on what I should be doing to avoid running into this error?

Should I be over-riding onCreateView() after all and inflate a custom list view (but I have no such need)?

EDIT:

Here’s how I add the default fragment to the main activity’s onCreate():

    FragmentManager fragmentManager = getFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    TitlesFragment fragment = (TitlesFragment) fragmentManager.findFragmentByTag(MAIN_SCREEN_TAG);
    if (fragment == null){
        fragment = TitlesFragment.newInstance(SCREEN_MAIN);
        fragmentTransaction.add(R.id.TitlesContainer, fragment, MAIN_SCREEN_TAG);

    } else {    
        fragmentTransaction.add(R.id.TitlesContainer, fragment);
    }

    fragmentTransaction.commit();

To perform the transition, this is what I do:

    fragment = (TitlesFragment) fragmentManager.findFragmentByTag(SECOND_FRAGMENT_TAG);
                if (fragment == null){

                    fragment = TitlesFragment.newInstance(SECOND_FRAGMENT);
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment, SECOND_FRAGMENT_TAG);
                } else {
                    fragmentTransaction.replace(R.id.TitlesContainer, fragment);
                }
    fragmentTransaction.commit();
  • 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-01T04:17:51+00:00Added an answer on June 1, 2026 at 4:17 am

    I got rid of the OnGlobalLayoutListener. Instead I set ‘layout_padding’ attribute on the first FrameLayout to set the margins on the Fragment that it encloses.

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

Sidebar

Related Questions

I am trying to understand how to use SyndicationItem to display feed which is
I need to clean up various Word 'smart' characters in user input, including but
I have a text area in my form which accepts all possible characters from
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
Basically, what I'm trying to create is a page of div tags, each has
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I used javascript for loading a picture on my website depending on which small
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the

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.