如果完美,我正在使用操作栏选项卡创建 Android 应用程序,但我对片段的替换方法有问题。如何解决此错误?
系统显示:
FragmentTransaction 类型中的方法 replace(int, Fragment) 不适用于参数 (int, Fragment)
开始活动.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;
}
}