Nikhil Soni
Nikhil Soni

Reputation: 119

Issue with fragment transaction

package com.androidnik.tourguide;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MustVisit extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_category);
    getSupportFragmentManager().beginTransaction().replace(R.id.container, new MustVisitFragment()).commit();
}

im getting this error : Error:(12, 80) error: incompatible types: MustVisitFragment cannot be converted to Fragment i have extended that fragment class within MyVisitFragment class and also have a empty constructor still im facing this problem

Upvotes: 1

Views: 95

Answers (3)

Ravi Rawal
Ravi Rawal

Reputation: 243

Add imports:

import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;

Will solve your issue.

Upvotes: 0

Amirshayan Aghamiri
Amirshayan Aghamiri

Reputation: 109

Replace android.R.id.content with R.id.container

Upvotes: 0

Vucko
Vucko

Reputation: 7479

The problem you're facing is with incompatibility between android.app.Fragment and support.v4.Fragment. These two are not the same, and each has it's own FragmentManager they work with.

You are using getSupportFragmentManager which means your fragment should extend support.v4.Fragment (check the imports, and change to this). Either change that or get the other fragment manager by calling getFragmentManager.

I'd personally suggest working with the support library, since in one of my projects, it proved good to me, while I had various problems with the other one, but this is purely my opinion.

Upvotes: 1

Related Questions