Blaze Phoenix
Blaze Phoenix

Reputation: 889

Cannot launch fragment: The specified child already has a parent

I am having the "child already has a parent" error close to what a lot of people have been getting, however, I am getting it doing something different (as far as my searches have shown me). I am assuming that the way I am adding the fragment is not necessarily wrong, but I cannot for the life of me figure out how the fragment I am adding already has a parent.

I am launching from my main activity using a fragment to be loaded into a FrameLayout. Here is the onCreate from my main activity:

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    realm = Realm.getDefaultInstance();

    if (savedInstanceState == null) {

    }

    tripsFragment = new CarTripsOverview();
    getSupportFragmentManager().beginTransaction().add(R.id.content_container, tripsFragment).commit();
}

And here are the methods which run for CarTripsOverview (which is a Fragment):

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    if (getArguments() != null) {

    }

    setUpRealm();
}

void setUpRealm() {
    realm = Realm.getDefaultInstance();
    RealmQuery<Car> carQuery = realm.where(Car.class);
    RealmResults<Car> carResults = carQuery.findAll();
    carAdapter = new CarArrayAdapter(getActivity(),0, carResults, true);

    RealmQuery<Trip> tripQuery = realm.where(Trip.class);
    RealmResults<Trip> tripResults = tripQuery.findAll();
    tripAdapter = new TripAdapterRealm(getActivity(), 0, tripResults, true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
    View view = inflater.inflate(R.layout.fragment_car_trips_overview, container, true);

    // Bind all of the views with Butter Knife
    ButterKnife.bind(this, view);

    setUpSwipeMenu();

    // Add my event listeners
    tripListView.setAdapter(tripAdapter);

    carSpinner.setAdapter(carAdapter);

    return view;
}

void setUpSwipeMenu() {
    SwipeMenuCreator creator = new SwipeMenuCreator() {
        @Override
        public void create(SwipeMenu menu) {
            SwipeMenuItem deleteItem = new SwipeMenuItem(getContext());
            deleteItem.setBackground(new ColorDrawable(Color.RED));
            deleteItem.setWidth(MathHelper.dipToPixels(getContext(), 90));
            deleteItem.setTitle("Delete");
            deleteItem.setTitleColor(Color.WHITE);
            deleteItem.setTitleSize(18);
            menu.addMenuItem(deleteItem);
        }
    };

    tripListView.setMenuCreator(creator);

    tripListView.setOnMenuItemClickListener(new SwipeMenuListView.OnMenuItemClickListener() {
        @Override
        public boolean onMenuItemClick(int position, SwipeMenu menu, int index) {
            Trip trip = tripAdapter.getItem(position);
            switch (index) {
                case 0:
                    delete(trip);
                    tripAdapter.notifyDataSetChanged();
                    break;
            }
            return false;
        }
    });
}

My biggest problem is that I just don't understand quite where I am adding the child to the parent where it would already have a parent.

All of my source code can be found here.

Upvotes: 0

Views: 63

Answers (1)

Daniel Zolnai
Daniel Zolnai

Reputation: 16910

Instead of:

View view = inflater.inflate(R.layout.fragment_car_trips_overview, container, true);

use:

View view = inflater.inflate(R.layout.fragment_car_trips_overview, container, false);

Notice the last parameter is false now. If you use true, the view you just created will be automatically added to the container (second parameter of inflate). With fragments, always use false in onCreateView(...).

Upvotes: 1

Related Questions