2016-07-18 47 views
-1

每當我動態地添加片段它顯示主活動錯誤它不解決FragmentManger類。當我添加片段動態它顯示錯誤

public class MainActivity extends AppCompatActivity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

    FragmentManager fragmentManager=getFragmentManager(); 
    FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction(); 
    FirstFragment ff= new FirstFragment(); 
    fragmentTransaction.add(R.id.fragment_container,ff); 
    fragmentTransaction.commit(); 
} 

感謝您提前幫助..

+1

使用getSupportFragmentManager()和片段。 –

回答

0

拌上V4庫的片段和v4.fragment

2
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction(); 
    transaction.add(R.id.container, MyFragment); 
    transaction.show(mapFragment); 
    transaction.commit();