Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager

I am creating an app using Fragments for tablet.I have so far created some buttons on the left side and the fragments appear on clicking the buttons.

But i am experiencing error in my MainActivity.java file "Type mismatch error:cannot convert from android.app.FragmentManager to android.support.v4.app.FragmentManager" And that is the main reason that i cannot implement it.I have already imported android.support.v4.app.FragmentManager.

How can i resolve my problem?

My code for MainActivity.java

      import android.app.Activity; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.View; import android.widget.Button;  public class MainActivity extends Activity {     Fragment fragment;     Button btn1,btn2,btn3,btn4,btn5,btn6;     @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_main);         btn1=(Button)findViewById(R.id.btn1);         btn2=(Button)findViewById(R.id.btn2);         btn3=(Button)findViewById(R.id.btn3);         btn4=(Button)findViewById(R.id.btn4);         btn5=(Button)findViewById(R.id.btn5);         btn6=(Button)findViewById(R.id.btn6);          FragmentManager fm=getFragmentManager();         FragmentTransaction ft=fm.beginTransaction();          StartFragment myfragment=new StartFragment();         ft.add(R.id.myfragment,myfragment);         ft.commit();         btn4.setOnClickListener(btnOnClickListener);         btn5.setOnClickListener(btnOnClickListener);         btn6.setOnClickListener(btnOnClickListener);     }            Button.OnClickListener btnOnClickListener=new Button.OnClickListener(){             @Override              public void onClick(View v)             {                 Fragment newfragment;                 if(v==btn1)                 {                     newfragment=new Fragment1();                 }                 else if(v==btn2)                 {                     newfragment=new Fragment2();                 }                 else if(v==btn3)                 {                     newfragment=new Fragment3();                 }                 else if(v==btn4)                 {                     newfragment=new Fragment4();                 }                 else if(v==btn5)                 {                     newfragment=new Fragment5();                 }                 else if(v==btn6)                 {                     newfragment=new Fragment6();                 }                 else                  {                     newfragment=new StartFragment();                 }                 FragmentTransaction transaction=getFragmentManager.beginTransaction();                 transaction.replace(R.id.myfragment,newfragment);                 transaction.addToBackStack(null);                 transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);                 transaction.commit();             }         };         } 
like image 602
sakshi Avatar asked May 22 '13 11:05

sakshi


1 Answers

You need to use getSupportFragmentManager() in your code, not getFragmentManager() - see here for more details.

You also need to extend your class from android.support.v4.app.FragmentActivity, not just from standard android.Activity to get this method.

like image 194
lepoetemaudit Avatar answered Oct 22 '22 13:10

lepoetemaudit