Type mismatch in Android for FragmentTransaction with getFragmentManager().beginTransaction()

StackOverflow https://stackoverflow.com/questions/19881810

سؤال

when i try replace the Fragment by declare FragmentTransaction ft = getFragmentManager().beginTransaction() in an activity, i always has the following error alert:

enter image description here

import android.support.v4.app.FragmentTransaction;

    public class MainFrameActivity extends ActivitySupport {

    FragmentTransaction ft = getFragmentManager().beginTransaction();

    switch (position) {
    case 0:
        ft.replace(R.id.content_frame, fragmentDiscover);
        break;
    case 1:
        ft.replace(R.id.content_frame, fragmentPersonInfo);
        break;
    }

    ft.commit();

    mDrawerList.setItemChecked(position, true);

    mDrawerLayout.closeDrawer(mDrawerList);
}

and the class is ActivitySupport inherited from:

public class ActivitySupport extends Activity implements IActivitySupport {

I've tried to change the getFragmentManager to getSupportFragmentManager(), and it's getting even worse to have the alert the method is not undefined.

So what can i do to make the getFragmentManager() to be a v4 supported method?

هل كانت مفيدة؟

المحلول 2

First extend FragmentActivity and make sure you are using the one from the support library.

Try using getSupportFragmentManager().beginTransaction() instead of getFragmentManager().beginTransaction().

The problem is that you have imported FragmentTransaction from v4 support library and you are not using the fragment manager from the v4 support library.

Here is the link for further refrence.

Hope this helps : )

نصائح أخرى

Change it

public class ActivitySupport extends Activity implements IActivitySupport {

To

public class ActivitySupport extends FragmentActivity implements IActivitySupport {

and then it will work

FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top