66

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();
            }
        };
        }

1 Answer 1

179

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.

Sign up to request clarification or add additional context in comments.

4 Comments

yes you can use what David has suggested.it worked in my case.
Thanks David for your kind help.It worked and now i am furthur working on it.
Worth noting that you can extend ActionBarActivity from your Activity now too.
If my class extends Fragment and i am using a FragmentPagerAdapter then what should i do?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.