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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T09:17:59+00:00 2026-06-17T09:17:59+00:00

I’m writing a simple Activity which should retrieve and show some data in a

  • 0

I’m writing a simple Activity which should retrieve and show some data in a Fragment containing a ListView. The problem is that I’m not able to access the ListView because none of the on* methods seems to be called.

The fragment has a LinearLayout which gets populated dynamically by the code in onCreateView. The Activity creates the Fragment in onCreate and adds it to its layout using the FragmentManager returned by getSupportFragmentManager(As a side note: I’m using the support-v4 library).

Adding some Log.d calls in the onAttach/onCreate/onCreateView methods clearly show that none of them is called at all.

What am I doing wrong?

The activity:

public class CharactersActivity extends FragmentActivity {

    private MainCharactersFragment mainFragment = null;

    private String[] charnames = null;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // R.layout.characters contains only a LinearLayout with id characters_fragment_container
        setContentView(R.layout.characters);
        if (mainFragment == null) {
            mainFragment = new MainCharactersFragment();
            //Add the fragment to the activity's layout.
            //Taken from Google's tutorials.
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.characters_fragment_container, mainFragment).commit();
            //I'd expect onCreateView to be called during this commit, but it is not called.
        }
        //Loads some data to display
        Intent intent = getIntent();
        String[] filenames = intent.getStringArrayExtra(PortableDnDActivity.CHARACTERS_FILENAMES);
        this.charnames = new String[filenames.length];
        for (int i=0; i < filenames.length; i++) {
            this.charnames[i] = getCharacterName(filenames[i]);
        }
    }

    @Override
    public void onStart() {
        // Set the ListAdapter to show the data.
        Log.d("MyLog", "           HERE            ");  // Shown by logs
        ListView view = (ListView) findViewById(R.id.main_characters_view); // This is null
        Log.d("MyLog", "The view is null:" + (view == null));
        mainFragment.setCharsAdapter(this, this.charnames);
        Log.d("MyLog", "                 HERE           2222"); //NOT shown by the logs
    }

The activity layout:

<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/characters_fragment_container"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    >
</LinearLayout>

The fragment:

public class MainCharactersFragment extends Fragment {

    public ListView charsList;
    public Button newCharButton;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // The following is never shown in the logs
        Log.d("MyLog", "                HERE                ");
        View view = inflater.inflate(R.layout.main_characters_fragment, container, false);
        return view;
    }

    public void setCharsAdapter(Context ctx, String[] data) {
        // Raise NullPointerException because at this moment
        // findViewById(R.id.main_characters_view) returns null
        charsList.setAdapter(new ArrayAdapter<String>(ctx, R.id.main_characters_view, data));
    }
    @Override
    public void onAttach(Activity activity) {
        Log.d("MyLog", "            onAttach called           "); //Never shown
    }
}

The fragment layout:

<?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">

    <ListView android:id="@+id/main_characters_view"
              android:layout_width="fill_parent"
              android:layout_height="0dp"
              android:layout_weight="0.5"
              />
    <Button android:id="@+id/new_character_button"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="@string/new_character"
            />

</LinearLayout>
  • 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-17T09:18:00+00:00Added an answer on June 17, 2026 at 9:18 am

    I think the NPE is confusing things. Comment that out of your onStart() so you can address it separately.

    Once you do that, you will see you have a few cases of falling to call the super method. Once in MainCharactersFragment.onAttach() and once in CharactersActivity.onStart(). Add those two calls in, clean, and try again.

    Given those changes, your code ran fine for me.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I am doing a simple coin flipping experiment for class that involves flipping a
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I am writing an app for my school newspaper, which is run completely online
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
For some reason, after submitting a string like this Jack’s Spindle from a text
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

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.