FragmentTransaction.replace() not working
来源:互联网 发布:用友网络未来价值2017 编辑:程序博客网 时间:2024/06/06 15:38
FragmentTransaction.replace() not working
I have a ListFragment
displaying a list of items that created by the user. I have a ListView
with it's id set to "@android:id/list" and a TextView
with the id "@android:id/empty".
I have an action bar button to display a fragment to allow the user to create more entries for the list. Here is the onOptionsItemSelected()
method:
@Overridepublic boolean onOptionsItemSelected(MenuItem item) { Fragment frag = null; // Right now I only have code for the addCourse() fragment, will add more soon switch (item.getItemId()) { case R.id.addCourse: frag = new AddCourseFragment(); break; default: return super.onOptionsItemSelected(item); } // The support library is being dumb and replace isn't actually getFragmentManager().beginTransaction() .remove(this).add(getId(), frag).addToBackStack(null).commit(); return true;}
The AddCourseFragment code is as follows:
public class AddCourseFragment extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View view = inflater.inflate(R.layout.fragment_addcourse, container, false);// if (view.findViewById(R.id.timesFrame)!=null) { Fragment timesFrag = new TimesFragment(); getChildFragmentManager().beginTransaction() .add(R.id.timesFrame, timesFrag).commit();// } return view; }}
As expected, when the list is unpopulated, android shows the text in the TextView
. I then hit the add course button and this happens:
It shows the empty text as well as the new fragment.
2 Answers
I had a similar problem.
According to this when you need to add Fragments
programmatically, you should add them to an existing ViewGroup
.
So I removed the Fragment
from the xml and used FrameLayout
component in the replace()
method.
You can also have a look here.
Hope this helps.
An other problem that solved the issue in my case were diffentent API imports.
Make sure, that you use "support.v4" or the "original" implementations from API 11 and do not mix them. (this can happen, in my case: TransactionManager was v4 and the Activity an older version, or the other way around)
- FragmentTransaction.replace() not working
- FragmentTransaction replace add
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the
- Android Error - The method replace(int, Fragment) in the type FragmentTransaction is not applicable
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable for the,
- 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the argum
- The method replace(int, Fragment) in the type FragmentTransaction is not applica
- The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the argument
- The method replace(int, Fragment) in the type FragmentTransaction is not app 解决方法
- The method replace(int, Fragment) in the type FragmentTransaction is not applica
- The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the argument
- The method replace(int, Fragment, String) in the type FragmentTransaction is not applicable...
- 错误:The method replace(int, Fragment) in the type FragmentTransaction is not applicable for the argum
- .replace(R.id.container, new User()).commit();/The method replace(int, Fragment) in the type FragmentTransaction is not app
- FragmentTransaction add 和 replace 区别
- FragmentTransaction add 和 replace 区别
- 解决fragmentTransaction.replace不能全屏
- FragmentTransaction add 和 replace 区别
- Leetcode|Word Ladder
- Struts2开发遇到的问题之多文件上传
- iOS github开源框架
- "Eclipse Compiler for Java " and "Oracle JDK"
- Spring的事务管理的几种方法
- FragmentTransaction.replace() not working
- 手斧Linux – 从LFS到Funtoo (102)
- Ubuntu14.04安装过程中出现《演示和完全安装》的选项,无法正确安装
- 黑马程序员——Java基础---线程的另一个总结(3)--互斥,线程锁
- 手斧Linux – 从LFS到Funtoo (103)
- 黑马程序员——C语言运算符的概念及分类
- ubuntu14.04(32位)下编译android4.4时遇到的问题
- SpringMVC,Servlet,Struts1,Struts2单多例笔记
- Spring IOC-WebApplicationContext刷新(was.refresh())