IllegalArgumentException:尝试 setView 时找不到片段 ID 的视图
IllegalArgumentException: No view found for id for fragment when trying to setView
当我试图 setview.I 知道问题出在第 3 行和第 4 行时,它说 IllegalArgumentException: No view found for id for fragment。它是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
当我试图 setview.I 知道问题出在第 3 行和第 4 行时,它说 IllegalArgumentException: No view found for id for fragment。它是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