I have MainActivity
with layout:
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity">
<FrameLayout
android:id="@+id/fragment2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1">
</FrameLayout>
</LinearLayout>
In the MainActivity.onCreate()
method I try to add a ListFragment
:
MyFragment themesFragment = new MyFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.add(R.id.fragment2, themesFragment);
ft.commit();
But then I get the following error:
Error:(19, 11) error: no suitable method found for add(int,MyFragment)
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; MyFragment cannot be converted to Fragment)
If I use android.app.ListFragment
adding succeeds, but I have problem with CursorLoader
.
What is best way load data from a database into a fragment, while adding and removing from activity by clicking a button?
if you use android.support.v4.ListFragment
, you must use getSupportFragmentManager()
instead of getFragmentManager()
Your activity must extends FragmentActivity