robinleathal
robinleathal

Reputation: 251

How to fix fragmentTransaction.replace error and cannot be converted to Fragment

I am learning Android from a couple of weeks.

I am now trying to use fragments. And using the ideas from https://github.com/dogriffiths/HeadFirstAndroid from book https://books.google.com/books?id=qkzrCQAAQBAJ&pg=PA275&lpg=PA275&dq=pure+java+class+fragments&source=bl&ots=Ayz-JbqS4r&sig=f2DBRNgX_wrvBnGrDhJiIfHNhrE&hl=en&sa=X&ved=0ahUKEwjD6OHS67rPAhUG1CYKHczIBnAQ6AEIMDAD#v=onepage&q=pure%20java%20class%20fragments&f=false

I am getting

'replace(int, android.app.fragment)' in 'android.app.fragmentTransaction' cannot be applied to

which is also

Error: ContactDetailFragment cannot be converted to Fragment

source code

import android.app.Activity;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;


    public class ContactActivity extends Activity implements ContactListFragment.ContactListListener{



        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_contact);


        }

        @Override
        public void itemClicked(long id){
            //method also defined in the listener
            View fragmentContainer = findViewById(R.id.fragment_detail_container);
            if (fragmentContainer != null){
                ContactDetailsFragment detailsFragment = new ContactDetailsFragment();
                FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
                detailsFragment.setContact(id);
                //fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment);
                fragmentTransaction.replace(R.id.fragment_detail_container, detailsFragment);
                fragmentTransaction.addToBackStack(null);
                fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                fragmentTransaction.commit();
            }

        }

    }

contactactivity.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_contact"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:baselineAligned="false"
    tools:context="esu.uco.rawal.p5rabina.ContactActivity">
        <fragment
            class="esu.uco.rawal.p5rabina.ContactListFragment"
            android:layout_width="0dp"
            android:layout_weight ="1"
            android:layout_height="match_parent"
            android:id="@+id/contact_list_frag"/>


        <FrameLayout
            android:id="@+id/fragment_detail_container"
            android:layout_width="0dp"
            android:layout_weight="2"
            android:layout_height="match_parent"
            >

        </FrameLayout>

</LinearLayout>

Upvotes: 0

Views: 1522

Answers (1)

kws
kws

Reputation: 966

Check you imports. You may use wrong packages.

There are two Fragment classes:

as well as two FragmentTransaction classes:

You can't use a v4 FragmentTransaction with a native Fragment or the opposite.

You may also want to read the Support Library Features docs.

Upvotes: 1

Related Questions