gpt4 book ai didi

android - ListFragment 中的异常 : The specified child already has a parent

转载 作者:行者123 更新时间:2023-11-29 21:51:34 24 4
gpt4 key购买 nike

我正在制作一个下载器应用程序,但在 onCreateView 返回部分之后出现了 child 的 parent 错误。我尝试了很多东西,但没有任何帮助。我试过:((ViewGroup)dlistView.getParent()).removeView(dlistView);之后编译器说:

01-12 12:05:15.558: E/AndroidRuntime(10740): java.lang.RuntimeException: Your content must have a ListView whose id attribute is 'android.R.id.list'

或删除容器/我得到一个 npe/或删除 V 但 V.getParent() 为空。

Which view is the parent, and which is the child?

代码:

public static class DownloadingFragment extends ListFragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DownloadingFragment() {
}

@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate (R.layout.list, null);
ListView dlistView = (ListView) V.findViewById(R.id.dlist);
List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
downloadInfo.add(new DownloadInfo("File", 1000));
DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
getActivity().getApplication(),R.layout.list, downloadInfo);
dlistView.setAdapter(diaa);
return dlistView; // throw exception
}
}
//...
}

预先感谢您的帮助。

编辑:新代码:

public static class DownloadingFragment extends Fragment {
public static final String ARG_SECTION_NUMBER = "section_number";
public DownloadingFragment() {
}
@Override
public View onCreateView (LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View V = inflater.inflate (R.layout.list, null);
return V;
}

@Override
public void onStart() {
// TODO Auto-generated method stub
super.onStart();
ListView dlistView = (ListView) getView().findViewById(R.id.dlist);
List<DownloadInfo> downloadInfo = new ArrayList<DownloadInfo>();
downloadInfo.add(new DownloadInfo("File", 1000));
DownloadInfoArrayAdapter diaa = new DownloadInfoArrayAdapter(
getActivity().getApplication(),R.layout.list, downloadInfo);
dlistView.setAdapter(diaa);
}
}
//...
}

最佳答案

那是因为您返回的 ListView 有一个父项(在 V 的布局树中的某处)。

对于任何类型的 fragment ,onCreateView 应该返回一个没有任何父级的 View 。

在这种情况下,因为它是一个 listFragment,所以创建 ListView (使用 xml 或以编程方式),然后返回它。不允许它有任何 parent ,因为它需要有一个 parent 作为 fragment 。

阅读here了解更多信息:

The system calls this when it's time for the fragment to draw its user interface for the first time. To draw a UI for your fragment, you must return a View from this method that is the root of your fragment's layout. You can return null if the fragment does not provide a UI.

关于android - ListFragment 中的异常 : The specified child already has a parent,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14292731/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com