应用程序应下载数据并将其提供给 listFragment obj,但它仅针对 ActionBar 下拉列表中的第一项“今天”执行此操作。当我选择第二个项目“Soon”时,应用程序崩溃并出现错误。
03-21 14:53:05.272: E/AndroidRuntime(5289): FATAL EXCEPTION: main
03-21 14:53:05.272: E/AndroidRuntime(5289): java.lang.IllegalStateException: commit already called
我知道commit()
多次调用会产生该错误,但没有提交数据不会显示在屏幕上。请帮助我,我不知道如何解决它。任何帮助将不胜感激。
公共类 MainActivity 扩展 Activity {
ParkFragment mFragment;
ParkFragment mFragment2;
FragmentTransaction transaction = getFragmentManager().beginTransaction();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final String[] actions = new String[] {"Today","Soon"};
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getBaseContext(), android.R.layout.simple_spinner_dropdown_item, actions);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST);
ActionBar.OnNavigationListener navigationListener = new OnNavigationListener() {
@Override
public boolean onNavigationItemSelected(int itemPosition, long itemId) {
if (actions[itemPosition] == "Today"){
if (mFragment2 != null){transaction.detach(mFragment2);}
if (mFragment == null){
mFragment = new ParkFragment("url1",MainActivity.this);
transaction.add(android.R.id.content, mFragment);
transaction.commit();
}else{
transaction.attach(mFragment);
transaction.commit();
}
}
if (actions[itemPosition]== "Soon"){
if (mFragment != null){transaction.detach(mFragment);
System.out.println("Detached");}
if (mFragment2 == null){
mFragment2 = new ParkFragment("url2",MainActivity.this);
transaction.add(android.R.id.content, mFragment2);
transaction.commit();
}else{
transaction.attach(mFragment2);
transaction.commit();
}
}
return true;
}
};
actionBar.setListNavigationCallbacks(adapter, navigationListener);
}