我在做什么 ::
发生了什么 ::
我可以第一次启动片段。
现在,当我第二次点击操作栏菜单项时。我是 得到错误,如日志
应该发生什么,我应该重新加载片段,如果它 已经在容器中
如果片段已添加,如何重新加载
MainActivity.java
public class MainActivity extends SherlockFragmentActivity implements OnItemClickListener{
private DrawerLayout drawlayout=null;
private ListView listview=null;
private ActionBarDrawerToggle actbardrawertoggle=null;
FragmentTransaction ft1 ;
FragSearch fragSearch=new FragSearch();
mFragSortingHome fragment1=new mFragSortingHome();
private String[] myfriendname=null;
private int[] photo=null;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.drawer_activity_main);
//Code moved to a seperate function from onCreate with a call in it for clarity
drawerConfig(savedInstanceState);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
actbardrawertoggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actbardrawertoggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if(item.getItemId()==android.R.id.home)
{
if(drawlayout.isDrawerOpen(listview))
{
drawlayout.closeDrawer(listview);
}
else {
drawlayout.openDrawer(listview);
}
}else if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
ft1.add(R.id.content_frame, fragSearch);
ft1.addToBackStack(null);
ft1.commit();
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getSupportMenuInflater();
menuInflater.inflate(R.menu.actionbar_sort_menu, menu);
return super.onCreateOptionsMenu(menu);
}
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3) {
selectItem(position);
}
private void drawerConfig(Bundle savedInstanceState) {
// TODO Auto-generated method stub
myfriendname = new String[] { "Home", "Login","List", "Photo", "Filter" };
photo = new int[] { R.drawable.ic_action_map, R.drawable.ic_action_accounts, R.drawable.ic_action_view_as_list, R.drawable.ic_action_photo, R.drawable.ic_action_photo};
drawlayout = (DrawerLayout)findViewById(R.id.drawer_layout);
listview = (ListView) findViewById(R.id.listview_drawer);
getSupportActionBar().setHomeButtonEnabled(true);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setDisplayShowTitleEnabled(true);
drawlayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
drawlayout.setBackgroundColor(Color.WHITE);
DrawerMenuListAdapter menuadapter=new DrawerMenuListAdapter(getApplicationContext(), myfriendname, photo);
listview.setAdapter(menuadapter);
actbardrawertoggle= new ActionBarDrawerToggle(this, drawlayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close)
{
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawlayout.setDrawerListener(actbardrawertoggle);
listview.setOnItemClickListener(this);
if (savedInstanceState == null) {
selectItem(0);
}
}
private void selectItem(int position) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
// Locate Position
switch (position) {
case 0:
mFragSortingHome fragment1=new mFragSortingHome();
ft.replace(R.id.content_frame, fragment1);/*
Bundle b = new Bundle();
b.putString("name",myfriendname[position]);
b.putInt("photo",photo[position]);
fragment1.setArguments(b);*/
break;
case 1:
FilterBuffetFragment fragment2=new FilterBuffetFragment();
ft.replace(R.id.content_frame, fragment2);/*
Bundle b1 = new Bundle();
b1.putString("name",myfriendname[position]);
b1.putInt("photo",photo[position]);
fragment2.setArguments(b1);*/
break;
case 2:
mFragment3 fragment3=new mFragment3();
ft.replace(R.id.content_frame, fragment3);/*
Bundle b2 = new Bundle();
b2.putString("name",myfriendname[position]);
b2.putInt("photo",photo[position]);
fragment3.setArguments(b2);*/
break;
case 3:
mFragment4 fragment4=new mFragment4();
ft.replace(R.id.content_frame, fragment4);/*
Bundle b3 = new Bundle();
b3.putString("name",myfriendname[position]);
b3.putInt("photo",photo[position]);
fragment4.setArguments(b3);*/
break;
case 4:
FilterBuffetFragment fragment5=new FilterBuffetFragment();
ft.replace(R.id.content_frame, fragment5);
ft.addToBackStack(null);/*
Bundle b4 = new Bundle();
b4.putString("name",myfriendname[position]);
b4.putInt("photo",photo[position]);
fragment5.setArguments(b4);*/
break;
}
ft.commit();
listview.setItemChecked(position, true);
/*setTitle(myfriendname[position]);*/
drawlayout.closeDrawer(listview);
}
}
我正在加载动作栏项目的片段oncick的代码是
}else if(item.getItemId()==R.id.searchID){
//SEARCH Button Handling
ft1=getSupportFragmentManager().beginTransaction();
ft1.hide(fragment1);
ft1.add(R.id.content_frame, fragSearch);
ft1.addToBackStack(null);
ft1.commit();
return true;
日志 ::
05-04 00:42:01.896: E/AndroidRuntime(2253): FATAL EXCEPTION: main
05-04 00:42:01.896: E/AndroidRuntime(2253): java.lang.IllegalStateException: Fragment already added: FragSearch{415a4748 #1 id=0x7f04003e}
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl.addFragment(FragmentManager.java:1159)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:616)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1444)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:429)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Handler.handleCallback(Handler.java:605)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Handler.dispatchMessage(Handler.java:92)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.os.Looper.loop(Looper.java:137)
05-04 00:42:01.896: E/AndroidRuntime(2253): at android.app.ActivityThread.main(ActivityThread.java:4340)
05-04 00:42:01.896: E/AndroidRuntime(2253): at java.lang.reflect.Method.invokeNative(Native Method)
05-04 00:42:01.896: E/AndroidRuntime(2253): at java.lang.reflect.Method.invoke(Method.java:511)
05-04 00:42:01.896: E/AndroidRuntime(2253): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
05-04 00:42:01.896: E/AndroidRuntime(2253): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
05-04 00:42:01.896: E/AndroidRuntime(2253): at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
使用FragmentManager.findFragmentById
或FragmentManager.findFragmentByTag
查找已添加的片段。如果这些方法返回null
,则片段尚未添加,您可以像现在一样继续。当这些方法找到片段时,您可以使用一个引用:设计片段类以使用reload
方法或类似方法,在这种情况下您可以调用它。