Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix the replace method error on Android?

I am creating Android application with action bar tab all the code if perfect but I have a problem with the replace method for the fragment. how to fix this error?

the system display:

The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the arguments (int, Fragment)

StartActivity.java

package com.devlebactionbartab;

import android.net.wifi.p2p.WifiP2pManager.ActionListener;
import android.os.Bundle;
import android.app.ActionBar;
import android.app.ActionBar.Tab;
import android.app.Activity;
import android.app.FragmentTransaction;
import android.drm.DrmStore.Action;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.Menu;

public class StartActivity extends FragmentActivity {

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


        ActionBar actionbar  = getActionBar();
        actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
        actionbar.setTitle("Tabbed Layout");


        ActionBar.Tab Frag1Tab = actionbar.newTab().setText("Fragment1");
        ActionBar.Tab Frag2Tab = actionbar.newTab().setText("Fragment2");
        ActionBar.Tab Frag3Tab = actionbar.newTab().setText("Fragment3");
        ActionBar.Tab Frag4Tab = actionbar.newTab().setText("Fragment4");

        Fragment fragment1 = new Fragment_1();
        Fragment fragment2 = new Fragment_2();
        Fragment fragment3 = new Fragment_3();
        Fragment fragment4 = new Fragment_4();

        Frag1Tab.setTabListener(new MyTabsListener(fragment1));
        Frag2Tab.setTabListener(new MyTabsListener(fragment2));
        Frag3Tab.setTabListener(new MyTabsListener(fragment3));
        Frag4Tab.setTabListener(new MyTabsListener(fragment4));

        actionbar.addTab(Frag1Tab);
        actionbar.addTab(Frag2Tab);
        actionbar.addTab(Frag3Tab);
        actionbar.addTab(Frag4Tab);
    }


class MyTabsListener implements ActionBar.TabListener{

    public Fragment fragment;

    //constractor
    public MyTabsListener (Fragment fragment){

        this.fragment = fragment;
    }

    @Override
    public void onTabReselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub
        ft.replace(R.id.fragment_container, fragment);
    }

    @Override
    public void onTabSelected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }

    @Override
    public void onTabUnselected(Tab tab, FragmentTransaction ft) {
        // TODO Auto-generated method stub

    }
}

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.start, menu);
        return true;
    }
}
like image 694
LebDev Avatar asked Oct 02 '13 15:10

LebDev


1 Answers

 android.app.FragmentTransaction.replace(int id,android.app.Fragment fragment);

Your method is looking for a Fragment that comes from android.app.Fragment, you have imported android.support.v4.app.Fragment. My sugguestion is you import your FragmentTransaction from v4 support as well.

steps:

DELETE: import android.app.FragmentTransaction;

ADD: import android.support.v4.app.FragmentTransaction;

The go to eclipse->project->clean -> clean all.

v4 support FragmentTransaction

like image 71
wtsang02 Avatar answered Sep 20 '22 00:09

wtsang02