Android: Añadir un fragmento simple

Soy bastante nuevo en las aplicaciones de Android, así que espero poder encontrar alguna ayuda aquí. Ya he buscado mi problema aquí y he encontrado algo, pero eso no funciona.

Quiero agregar un fragmento a un FrameLayout pero no funciona. Mi objetivo es crear un Frame (/ Framework?) Que esté siempre presente y el usuario pueda interactuar con él y dentro de este Frame en una "ventana" específica Quiero mostrar páginas / fragmentos, cinco en total, y beeing capaz de cambiar Las páginas / fragmentos en cualquier momento, así que tengo un marco siempre presente y dentro de este cambio dinámico páginas. Pero por ahora estoy atascado al principio con la adición de un fragmento simple a este Marco (que ya está trabajando btw.)

Este es todo el código relevante espero: El error se produce en MainActivity.java (getSupportFragmentManager (). BeginTransaction (). Add (R.id.mainFrame, homeFragment) .commit ();) donde me dice:

FragmentTransaction.add (Fragment, String) no es aplicable (argumento desajustado; int no se puede convertir en Fragmento) método FragmentTransaction.add (int, Fragmento) no es aplicable (falta de coincidencia de argumento, el fragmento de inicio no se puede convertir en fragmento)

Ya he intentado emitir HomeFragment a Fragment, pero eso no funcionó.

MainActivity.java

import android.app.FragmentTransaction; import android.content.Intent; import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainActivity extends FragmentActivity { FragmentTransaction fragmentTransaction; HomeFragment homeFragment; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); homeFragment = new HomeFragment(); **getSupportFragmentManager().beginTransaction().add(R.id.mainFrame, homeFragment).commit();** } 

Activity_main.xml

 <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/container" android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".MainActivity" tools:ignore="MergeRootFrame" > <FrameLayout android:id = "@+id/mainFrame" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_marginBottom = "@dimen/bottom_Main_Tabs"> </FrameLayout> [...] </FrameLayout> 

Fragment_home.xml

 <FrameLayout 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" tools:context="com.example.HomeFragment"> // it is not really com.example... <!-- TODO: Update blank fragment layout --> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@string/hello_blank_fragment" /> </FrameLayout> 

HomeFragment.java (todo se autogeneró todavía, pero ya he cortado algo)

 public class HomeFragment extends Fragment { private OnFragmentInteractionListener mListener; public static HomeFragment newInstance() { HomeFragment fragment = new HomeFragment(); return fragment; // not really neccessary, because it Have shortened it } public HomeFragment() { // Required empty public constructor } @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // Inflate the layout for this fragment return inflater.inflate(R.layout.fragment_home, container, false); } // 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(Activity activity) { super.onAttach(activity); try { mListener = (OnFragmentInteractionListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnFragmentInteractionListener"); } } @Override public void onDetach() { super.onDetach(); mListener = null; } public interface OnFragmentInteractionListener { // TODO: Update argument type and name public void onFragmentInteraction(Uri uri); } } 

¿Puede alguien ayudarme por favor?

John

Estás mezclando clases de la biblioteca de soporte y las nuevas clases disponibles sólo para versiones más recientes del sistema operativo.

Por ejemplo, importar android.app.FragmentTransaction (disponible para API 11+) pero la llamada a getSupportFragmentManager().beginTransaction() devuelve android.support.v4.app.FragmentTransaction

Necesitas importar android.support.v4.app.FragmentTransaction y asegurarte de que tu HomeFragment extienda android.support.v4.app.Fragment y no android.app.Fragment .

FlipAndroid es un fan de Google para Android, Todo sobre Android Phones, Android Wear, Android Dev y Aplicaciones para Android Aplicaciones.