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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T12:28:06+00:00 2026-06-09T12:28:06+00:00

I have a 2 pane view that is defined in XML. For xlarge, the

  • 0

I have a 2 pane view that is defined in XML. For xlarge, the left side of the view is a Fragment that deals with content selection. I want to initialize this selector ListFragment to have the first item highlighted on app start up.

I’ve written a method in the ListFragment to do this:

public void setSelectedItem (int position) {
    //Unselect the previous item
    if(mPreviousPosition != ListView.INVALID_POSITION) {
        View oldSelected = getListView().getChildAt(mPreviousPosition);
        oldSelected.setBackgroundResource(R.drawable.menu_button);
        ((ImageView) oldSelected.findViewById(R.id.selector_icon)).setImageDrawable(icons.getDrawable(mPreviousPosition));
        ((TextView) oldSelected.findViewById(R.id.selector_text)).setTextAppearance(getActivity(), R.style.text_selector);
    }

    //Select this item
    View newSelected = getListView().getChildAt(position);
    newSelected.setBackgroundResource(R.drawable.menu_button_active);
    ((ImageView) newSelected.findViewById(R.id.selector_icon)).setImageDrawable(icons_active.getDrawable(position));
    ((TextView) newSelected.findViewById(R.id.selector_text)).setTextAppearance(getActivity(), R.style.text_selector_active);

    mPreviousPosition = position;
}

When I call this after an item has been selected, it works great. But when I try highlight the first item on startup, I get a NPE at

newSelected.setBackgroundResource(R.drawable.menu_button_active);

It seems getListView().getChildAt(position) is returning null because the Fragment’s view hasn’t been created yet.

So, from where in my Activity is it safe to try initialise the ListFragment? I’ve tried in onStart, onRestoreInstanceState and onResume but they all produce the NPE.

  • 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-09T12:28:06+00:00Added an answer on June 9, 2026 at 12:28 pm

    From what I understand you have two panes (left pane is ListFragment) and these two fragments are held within a FragmentActivity. The fragments are being set in the xml layout for the FragmentActivity. The fragments are available to the FragmentActivity following the onAttach() cycle but this doesn’t necessarily mean that the view elements in the child fragment are available yet.

    First question is where are you trying to set the selected item? In the FragmentActivity or in the ListFragment? It should be handled in the ListFragment in the onResume() method.

    I take it that based on the selection in the ListFragment you are then populating the right fragment with data for the selection. You will probably want to look at this link which will give you a good example of using listeners to avoid dependancies between the fragment activity and underlying fragments

    http://developer.android.com/training/basics/fragments/communicating.html

    You can then use listeners to handle the selection in the ListFragment and pass the selection to the right fragment. In the right fragment you can check for a selection in the FragmentActivity as well as register the fragment to listen for selections. This way you should cover the situation where the selection is fired before the right fragment has registered as a listener.

    UDPATE

    Look at responsive layout design for further information but here are some starting points.

    /res
        /values
           bool.xml
        /values-sw600dp-land
           bools.xml
    

    Then inside the bools.xml file define a value for isDualPane

    <resources>
        <bool name="isDualPane">[true/false]</bool>
    </resources>
    

    Then in your code in the listFragment you can then reference bools resource and know what the layout is.

    isDualPane = getResources().getBoolean(R.bool.isDualPane);
    

    You also have the benefit of using this in any class to know what the layout is and without needing to create dependancies between classes.

    Here are some links that might help.

    http://developer.android.com/training/multiscreen/screensizes.html
    http://developer.android.com/training/multiscreen/adaptui.html

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

Sidebar

Related Questions

I have a single view which has a menu on the left hand side
I have an option pane that is displayed upon closing my Application ( windowClosing()
I have a menu-bar and a tabbed pane in a frame, and i want
I have a fiddle here - http://jsfiddle.net/hhimanshu/SDr3F/2/ The left pane is already available I
I am creating a tabbed pane for the amount of rooms that I have
I have an iPad Split View Application that brings up a modal view to
I have a navigation controller in the master pane of a split view controller.
I have a main form with a left and right pane. The left pane
I have an Asp.net MVC partial view that is used for searching. It does
Hello i have that grid pane and there is labels and textboxes in that

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.