Sono abbastanza nuovo per le app Android, quindi spero di trovare qualche aiuto qui. Ho già cercato il mio problema qui e ho trovato qualcosa, ma questo non funziona.Android: aggiunta di un frammento semplice
Desidero aggiungere un frammento a un FrameLayout ma non funziona. Il mio obiettivo è creare un Frame (/ Framework?) Che sia sempre presente e l'utente possa interagire con esso e all'interno di questo Frame in una "finestra" specifica Voglio mostrare pagine/frammenti, cinque in totale, e essendo in grado di passare le pagine/i frammenti in qualsiasi momento, quindi ho un Frame sempre presente e all'interno di queste pagine che cambiano dinamicamente. (., Che sta già lavorando btw) Ma per ora mi sono bloccato proprio all'inizio, con l'aggiunta di un semplice frammento di questa immagine Pagina
Questo è tutto il codice relativo spero: L'errore si verifica nel MainActivity.java (. getSupportFragmentManager() beginTransaction() aggiungere (R.id.mainFrame, homeFragment) .commit();) dove mi dice:.
Error:(25, 55) error: no suitable method found for add(int,HomeFragment) method FragmentTransaction.add(Fragment,String) is not applicable (argument mismatch; int cannot be converted to Fragment) method FragmentTransaction.add(int,Fragment) is not applicable (argument mismatch; HomeFragment cannot be converted to Fragment)
ho già provato a lanciare homeFragment di frammentare, ma che non ha funzionato .
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 (tutto è generato automaticamente ancora, ma ho già tagliato fuori qualcosa)
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);
}
}
Qualcuno può aiutarmi per favore?
John
Il rispondente ha ragione, il problema è che non si sta utilizzando la libreria di supporto per ogni classe correlata a Fragment. – EpicPandaForce