[英]findViewById in fragment , how to get view outside onCreateView
Simple json parsing, but i can't do it cause whenever I would like build it, it crashes, I think it's because of "findViewById". 简单的json解析,但是无论何时我想构建它,我都无法做到,它崩溃了,我认为是因为“ findViewById”。
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
showDrugs(null);
return inflater.inflate(R.layout.fragment_first, container, false);
}
private void showDrugs(String search) {
ListView lvDrugs = (ListView) getView().findViewById(R.id.lvDrugs);
MyRealm myRealm = new MyRealm(getActivity().getApplicationContext());
RealmResults<Drug> drugs = myRealm.getDrugs(search);
DrugListAdapter adapter = new DrugListAdapter(getActivity().getApplicationContext(), drugs);
lvDrugs.setAdapter(adapter);
}
You cannot call showDrugs()
before inflating the view because getView()
is null
. 您不能在放大视图之前调用
showDrugs()
,因为getView()
为null
。
Simplest way is to call showDrugs()
in onViewCreated()
, which is called right after onCreateView()
, where you can be sure that getView()
isn't null
. 最简单的方法是调用
showDrugs()
在onViewCreated()
这是之后称为onCreateView()
在那里你可以肯定的是getView()
是不是null
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.