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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T16:30:56+00:00 2026-06-15T16:30:56+00:00

I have an fragment on activity, with an listview element on it. Can’t add

  • 0

I have an fragment on activity, with an listview element on it. Can’t add data to listview. Can someone tell me what is wrong?

public class MainActivity extends FragmentActivity {

    public String[] listS ={"item1","item2","item3"};

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main); 
        FragmentManager fm = getSupportFragmentManager();
        FragmentTransaction ft = fm.beginTransaction();
        MainFragment f = new MainFragment();
        ft.replace(R.id.fragcontainer, f);
        ft.commit();    
        //here the problem occurs
        ListView lv = (ListView)findViewById(R.id.listf);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

Fragment’s class:

public class MainFragment extends Fragment {
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle sis){
        return inflater.inflate(R.layout.fragment_layout, container, false);
    }

}
  • 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-15T16:30:58+00:00Added an answer on June 15, 2026 at 4:30 pm

    There’s a number of problems with this method, but the root of it is that the FragmentTransaction replace() method that adds the fragment that, I assume, contains the ListView you’re trying to find is an asynchronous process. The fragment isn’t added to the Activity’s view hierarchy immediately but some time later.

    If you want to add things to a ListView in a Fragment, why not do it from within the MainFragment subclass’ onCreateView() method instead? That’s how it’s intended to work.

    EDIT: A typical onCreateView():

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_layout, container, false);
        ListView lv = (ListView) view.findViewById(R.id.listf);
        lv.setAdapter(new ArrayAdapter<String>(this, R.layout.list_row,R.id.labeld, listS));             
        return view;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a very simple activity just to show the preference fragment: public class
I have class that extend FragmentActivity in it I add fragment to layout as
I have a custom ArrayAdapter populating a ListView . Here it is: public class
public class MainActivity extends Activity implements MainMenuFragment.OnMainMenuItemSelectedListener { @Override public void onCreate(Bundle savedInstanceState) {
I have a custom ArrayAdapter I use to manage my ListView: public class FilesAdapter
I've just started converting my apps to use fragments. I have a ListView activity
I have an Activity which uses a Fragment . I simply want to pass
I have an activity with 2 fragments. I have added custom animations for fragment
I have the next sequence: create activity; place a fragment into it; go to
can i use fragments and map view in the same activity. I have seen

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.