findViewById在片段中,如何在onCreateView之外获取视图

时间:2016-11-26 18:59:23

标签: java android listview parsing adapter

简单的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);
}

1 个答案:

答案 0 :(得分:1)

在充气视图之前,您无法致电showDrugs(),因为getView()null

最简单的方法是在showDrugs()中调用onViewCreated()onCreateView()后调用getView(),您可以确定null不是 function(nome) { console.log(nome); $http.get('/botaopermissao/'+nome).success(function(data) { console.log(data); if(data == "Encontrado!"){ $scope.botoa= "what you want"; } } }); } }}