我正在开发的应用程序有一个带有5个标签(片段)的操作栏。其中一个片段显示警告对话框,但布局为空白。我想放一个背景图片,所以我为该片段创建了一个布局,并使用inflater.inflate(...)方法来设置布局。
问题是该行代码将布局设置为所有片段。我怎样才能将它仅限于我需要的片段?这是我的代码:
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
//...
AlertDialog ad = new AlertDialog.Builder(getActivity()).setTitle("Title")
//...
ad.show();
inflater.inflate(R.layout.fragment_my, container); //this is the layout I want to inflate to my fragment
return super.onCreateView(inflater, container, savedInstanceState);
}
还尝试用以下代码替换最后两行:
return inflater.inflate(R.layout.fragment_trovami, container);
但是我收到了这个错误:
07-27 16:19:46.768: E/AndroidRuntime(1998): java.lang.IllegalStateException:
The specified child already has a parent. You must call removeView() on the child's parent first.
答案 0 :(得分:1)
答案:
return inflater.inflate(R.layout.fragment_trovami, container,false);