MainActivity类型中的方法pushFragments(String,Fragment)不适用于参数(String,Fragment1)

时间:2013-09-23 12:31:46

标签: android android-fragments

我收到错误“MainActivity类型中的方法pushFragments(String,Fragment)不适用于参数(String,Fragment1)”

不确定我为什么会收到此错误,希望有人可以帮助我。

MainActivity:

public class MainActivity extends FragmentActivity {

private final String TAB_A = "All";
private final String TAB_B = "Categories";
private final String TAB_C = "Popular";
private final String TAB_D = "About";

private String selectedTab;

private static TabHost mTabHost;

private Fragment1 fragment1;
private Fragment2 fragment2;
private Fragment3 fragment3;
private Fragment4 fragment4;

@Override
protected void onCreate(Bundle savedInstanceState) {
    this.requestWindowFeature(Window.FEATURE_NO_TITLE);
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    fragment1 = new Fragment1();
    fragment2 = new Fragment2();
    fragment3 = new Fragment3();
    fragment4 = new Fragment4();

    mTabHost = (TabHost) findViewById(android.R.id.tabhost);
    mTabHost.setOnTabChangedListener(listener);
    mTabHost.setup();

    initializeTab();
}

// TABS

public void initializeTab() {
    TabHost.TabSpec spec = mTabHost.newTabSpec(TAB_A);
    mTabHost.setCurrentTab(0);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_A, R.drawable.tab1_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_B);

    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }

    });

    spec.setIndicator(createTabView(TAB_B, R.drawable.tab2_selector));
    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_C);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_C, R.drawable.tab3_selector));

    mTabHost.addTab(spec);

    spec = mTabHost.newTabSpec(TAB_D);
    spec.setContent(new TabHost.TabContentFactory() {
        public View createTabContent(String tag) {
            return findViewById(android.R.id.tabcontent);
        }
    });
    spec.setIndicator(createTabView(TAB_D, R.drawable.tab4_selector));
    mTabHost.addTab(spec);

    mTabHost.setCurrentTab(0);
}

TabHost.OnTabChangeListener listener = new TabHost.OnTabChangeListener() {
    public void onTabChanged(String tabId) {
        /* Set current tab.. */
        if (tabId.equals(TAB_A)) {

            //ERROR HERE:
            pushFragments(tabId, fragment1);
            selectedTab = TAB_A;
        } else if (tabId.equals(TAB_B)) {
            pushFragments(tabId, fragment2);
            selectedTab = TAB_B;
        } else if (tabId.equals(TAB_C)) {
            pushFragments(tabId, fragment3);
            selectedTab = TAB_C;
        } else if (tabId.equals(TAB_D)) {
            pushFragments(tabId, fragment4);
            selectedTab = TAB_D;
        }
    }
};


public void pushFragments(String tag, Fragment fragment) {

    FragmentManager manager = getSupportFragmentManager();
    FragmentTransaction ft = manager.beginTransaction();

    ft.replace(android.R.id.tabcontent, fragment);
    ft.commit();
}


private View createTabView(final String tabText, final int id) {
    View view = LayoutInflater.from(this).inflate(R.layout.tabs_icon, null);
    ImageView imageViewTabIcon = (ImageView) view
            .findViewById(R.id.tab_icon);
    imageViewTabIcon.setImageDrawable(getResources().getDrawable(id));

    return view;
}

public static void tabfresh() {
    mTabHost.setCurrentTab(0);
}

public static void tabfresh_sal() {
    mTabHost.setCurrentTab(1);
}
}

片段类:

public class Fragment1 extends FragmentActivity {
private static final String TAG = "Fragment1";

@Override
protected void onCreate(Bundle savedInstanceState) {
    if (BuildConfig.DEBUG) {
        Utils.enableStrictMode();
    }
    super.onCreate(savedInstanceState);

    if (getSupportFragmentManager().findFragmentByTag(TAG) == null) {
        final FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
        ft.add(android.R.id.content, new ImageGridFragment(), TAG);
        ft.commit();
    }
}
}

0 个答案:

没有答案