无法在Android

时间:2016-07-16 12:43:21

标签: java android android-layout android-fragments fragmenttransaction

这是主要的活动布局

<?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>

和活动

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>

和片段类

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);
    }
}

当我尝试添加fagment fragmentTransaction.add(R.id.layout_calendar,calendar)时,该函数与任何函数都不匹配,但是add函数接收一个int和一个片段(CalendarFragment扩展Fragmnet),看起来是正确的,但它没有&# 39;吨

1 个答案:

答案 0 :(得分:2)

问题是你正在使用:

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

您的Fragment类从Fragment库扩展support.v4

import android.support.v4.app.Fragment;

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

FragmentManager fm = getFragmentManager();

这样,您将从支持库中获得FragmentManager。另外,不要忘记用支持替换导入。