c'est la principale disposition de l'activiténe peut pas ajouter Fragment sur android
<?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>
et l'activité
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;
}
}
}
la mise en page de fragment
<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>
et la classe fragment
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);
}
}
Lorsque j'essaie d'ajouter le fagment fragmentTransaction.add(R.id.layout_calendar,calendar)
la fonction ne correspond à aucune fonction, mais ajouter une fonction reçoit un int et un fragment (CalendarFragment étend Fragmnet), semble être correct mais ce n'est pas le cas.
Que dois-je utiliser, android.app.Fragment ou android.support.v4.app.Fragment? – AFS
Vous pouvez probablement trouver en ligne de nombreuses discussions sur ce sujet pour savoir quel est le meilleur et pourquoi (longue histoire courte - ils sont assez similaires). Ce qui est certain, c'est que vous ** ne devez pas mélanger ** les éléments de 'android.app' et' support.v4'. Une fois que vous décidez, continuez. Personnellement, je préfère la bibliothèque de support dans mes projets. – Vucko
Je pense qu'il serait plus intelligent d'essayer de chercher l'erreur par vous-même, puis de poser une nouvelle question avec un logcat complet, parce que je ne peux pas le réparer comme ça depuis les commentaires, désolé:/ – Vucko