无法在 android 上添加片段

Cannot add Fragment on android

这是主要的activity布局

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    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"
    tools:context="gw.es.lasarenas.MainActivity">

    <fragment
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:name="gw.es.lasarenas.CalendarFragment"
        android:id="@+id/fragment"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true" />
</RelativeLayout>

和activity

package gw.es.lasarenas;


import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;

import java.net.URL;

import javax.mail.MessagingException;

public class MainActivity extends FragmentActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        FragmentManager fm = getFragmentManager();
        FragmentTransaction fragmentTransaction = fm.beginTransaction();
        CalendarFragment calendar = new CalendarFragment();
        fragmentTransaction.add(R.id.layout_calendar,calendar);//here is the proble
        fragmentTransaction.commit();
        //new Email().execute();
    }

    private class Email extends AsyncTask<URL, Integer, Long> {

        @Override
        protected Long doInBackground(URL... params) {
            SendMail mail = new SendMail();
            try {
                mail.sendEmail();
            } catch (MessagingException e) {
                e.printStackTrace();
            }
            return null;
        }
    }

}

片段布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:id="@+id/layout_calendar"
    android:layout_height="fill_parent"
    android:orientation="vertical"
    tools:context="gw.es.lasarenas.CalendarFragment">

    <!-- TODO: Update blank fragment layout -->

    <CalendarView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/calendarView"
        android:layout_gravity="center_horizontal" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/next"
        android:id="@+id/button"
        android:layout_gravity="center_horizontal" />
</LinearLayout>

和片段class

package gw.es.lasarenas;

import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


/**
 * A simple {@link Fragment} subclass.
 * Activities that contain this fragment must implement the
 * {@link CalendarFragment.OnFragmentInteractionListener} interface
 * to handle interaction events.
 */
public class CalendarFragment extends Fragment {

    private OnFragmentInteractionListener mListener;

    public CalendarFragment() {
        // Required empty public constructor
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_calendar, container, false);

        return view;
    }

    // TODO: Rename method, update argument and hook method into UI event
    public void onButtonPressed(Uri uri) {
        if (mListener != null) {
            mListener.onFragmentInteraction(uri);
        }
    }

    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
        if (context instanceof OnFragmentInteractionListener) {
            mListener = (OnFragmentInteractionListener) context;
        } else {
            throw new RuntimeException(context.toString()
                    + " must implement OnFragmentInteractionListener");
        }
    }

    @Override
    public void onDetach() {
        super.onDetach();
        mListener = null;
    }

    /**
     * This interface must be implemented by activities that contain this
     * fragment to allow an interaction in this fragment to be communicated
     * to the activity and potentially other fragments contained in that
     * activity.
     * <p>
     * See the Android Training lesson <a href=
     * "http://developer.android.com/training/basics/fragments/communicating.html"
     * >Communicating with Other Fragments</a> for more information.
     */
    public interface OnFragmentInteractionListener {
        // TODO: Update argument type and name
        void onFragmentInteraction(Uri uri);
    }
}

当我尝试添加片段时 fragmentTransaction.add(R.id.layout_calendar,calendar) 该函数与任何函数都不匹配,但添加函数接收一个 int 和一个片段(CalendarFragment extends Fragmnet),看起来它是正确的但它不t.

问题是您使用的是:

import android.app.FragmentManager;
import android.app.FragmentTransaction;

并且您的片段 class 从 support.v4 库扩展 Fragment

import android.support.v4.app.Fragment;

这会导致错误,因为您不能将这两者混用。将所有内容更改为其中之一,例如在此行中使用 getSupportFragmentManager 而不是 getFragmentManager

FragmentManager fm = getFragmentManager();

这样,您将从支持库中获得 FragmentManager。也不要忘记用来自支持的导入替换导入。