它说IllegalArgumentException:当我尝试setview时,找不到片段ID的视图。我知道问题出在第三和第四行。它是News.java文件新闻是一个片段。
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getActivity().setContentView(R.layout.fragment_news);
listView = (ListView) getActivity().findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
最佳答案
Fragment.java文件类似于以下代码:
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_news, container, false);
//ButterKnife.bind(this, view);
init(view);
return view;
}
之后,在
init
方法中键入以下代码:private void init(View view){
listView = (ListView) view.findViewById(R.id.list);
feedItems = new ArrayList<FeedItem>();
listAdapter = new FeedListAdapter(this, feedItems);
listView.setAdapter(listAdapter);
}
您也不要忘记
extends Fragment
关于android - IllegalArgumentException:尝试setView时找不到 fragment ID的 View ,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/49207102/