this is the main activity layout
<?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>
and the 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;
}
}
}
the fragment layout
<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>
and the fragment 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);
}
}
When I try to add the fagment fragmentTransaction.add(R.id.layout_calendar,calendar)
the function doesn't match with any funcion ,but add function receive a int and a fragment (CalendarFragment extends Fragmnet),looks like its correct but it doesn't.
The problem is that you're using :
import android.app.FragmentManager;
import android.app.FragmentTransaction;
And your Fragment class extends Fragment
from the support.v4
library:
import android.support.v4.app.Fragment;
This causes the error because you cannot mix these two. Change everything to either one or the other, for example use getSupportFragmentManager
instead of getFragmentManager
in this line:
FragmentManager fm = getFragmentManager();
This way, you'll get a FragmentManager
from the support library. Also don't forget to replace the imports with the ones from support also.