0

MainActivity與佈局:如何將android.support.v4.app.ListFragment添加到活動?

<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> 

MainActivity.onCreate()方法我嘗試添加ListFragment

MyFragment themesFragment = new MyFragment(); 
     FragmentTransaction ft = getFragmentManager().beginTransaction(); 
     ft.add(R.id.fragment2, themesFragment); 
     ft.commit(); 

但後來我得到以下錯誤:

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) 

如果我使用android.app.ListFragment添加成功,但我有問題CursorLoader

通過單擊按鈕添加和刪除活動時,將數據從數據庫加載到片段中的最佳方式是什麼?如果你使用android.support.v4.ListFragment

+0

給標籤,就可以片段和使用。 – Keshav1234

+0

只需檢查你是如何導入你的'片段'? – Piyush

回答

0

,您必須使用getSupportFragmentManager()代替getFragmentManager()

你的活動必須延伸FragmentActivity