这里我的问题不好解释。首先选择第一个选项卡,然后如果我点击第二个选项卡,它的工作正常,但再次单击第一个选项卡我的应用程序将被关闭。你可以建议我吗? 这是我的代码。
public class MainActivity extends Activity {
public static Context appContext;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
appContext = getApplicationContext();
ActionBar actionbar = getActionBar();
actionbar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
ActionBar.Tab connectionTab = actionbar.newTab().setText("Connection");
ActionBar.Tab masterDataTab = actionbar.newTab().setText("Master Data");
Fragment connection = new AFragment();
Fragment masterData = new BFragment();
connectionTab.setTabListener(new MyTabsListener(connection));
masterDataTab.setTabListener(new MyTabsListener(masterData));
actionbar.addTab(connectionTab);
actionbar.addTab(masterDataTab);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
}
return false;
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("tab", getActionBar().getSelectedNavigationIndex());
}
}
class MyTabsListener implements ActionBar.TabListener {
public Fragment fragment;
public MyTabsListener(Fragment fragment) {
this.fragment = fragment;
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
Toast.makeText(MainActivity.appContext, "Reselected!", Toast.LENGTH_LONG).show();
//ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
ft.replace(R.id.fragment_container, fragment);
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
ft.remove(fragment);
}
}