如何修复fragmentTransaction.replace错误并且无法转换为Fragment

时间:2016-10-02 19:07:26

标签: android android-fragments android-listfragment

我将在几周内学习Android。

我现在正在尝试使用片段。并使用https://github.com/dogriffiths/HeadFirstAndroid中的想法 从书中 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

我正在

  

' replace(int,android.app.fragment)'在   ' android.app.fragmentTransaction'无法应用于

也是

  

错误:无法将ContactDetailFragment转换为Fragment

源代码

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>

1 个答案:

答案 0 :(得分:1)

检查您的进口情况。您可能使用了错误的包裹。

有两个Fragment类:

以及两个FragmentTransaction类:

您不能将v4 FragmentTransaction与原生Fragment一起使用或相反。

您可能还想阅读Support Library Features文档。