用片段调用onRefresh中的findViewById

时间:2015-11-17 16:52:55

标签: android android-fragments

当我刷新SwipeRefreshLayout时,我试图在我的布局中设置一些视图。 但是,当我试图拨打NullPointerException时,我会继续findViewById。 我的代码:

public class LecturerFragment extends Fragment
    implements SwipeRefreshLayout.OnRefreshListener {

    private SwipeRefreshLayout swipeRefreshLayout;
    private View rootView;


    private OnFragmentInteractionListener mListener;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        rootView = inflater.inflate(R.layout.fragment_lecturer, container, false);


        return rootView;
    }


    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {

        if (savedInstanceState == null) {
            swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_lecturer);
            swipeRefreshLayout.post(new Runnable() {
                @Override
                public void run() {
                    swipeRefreshLayout.setRefreshing(true);
                }
            });
            swipeRefreshLayout.setColorSchemeResources(R.color.refresh_1, R.color.refresh_2, R.color.refresh_3, R.color.refresh_4);
            swipeRefreshLayout.setOnRefreshListener(this);

            this.setLecturers();
        }
    }



    private void setLecturers() {
        String apiUrl = this.getResources().getString(R.string.api_url);

        RestAdapter restAdapter = new RestAdapter.Builder().setEndpoint(apiUrl).build();

        LecturerApi api = restAdapter.create(LecturerApi.class);

        api.getLecturers(new Callback<ArrayList<LecturerModel>>() {
            @Override
            public void success(ArrayList<LecturerModel> lecturerModels, Response response) {
                //some setter code

                swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_refresh_lecturer);
                swipeRefreshLayout.setRefreshing(false);
            }

            @Override
            public void failure(RetrofitError error) {
                Toast.makeText(getActivity(), "Failed to fetch lecturers data", Toast.LENGTH_LONG).show();

                swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_refresh_lecturer);
                swipeRefreshLayout.setRefreshing(false);
            }
        });

    }

    @Override
    public void onRefresh() {
        this.setLecturers();
    }
}

每次调用findViewById方法时,我都会收到错误。

2 个答案:

答案 0 :(得分:2)

使用您正在膨胀的视图(rootView)作为参考,而不是getView()

使用

swipeRefreshLayout = (SwipeRefreshLayout) rootView.findViewById(R.id.swipe_refresh_lecturer);

而不是

swipeRefreshLayout = (SwipeRefreshLayout) getView().findViewById(R.id.swipe_refresh_lecturer);

答案 1 :(得分:2)

仅指定swipeRefreshLayout一次:创建片段视图时:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    rootView = inflater.inflate(R.layout.fragment_lecturer, container, false);
    swipeRefreshLayout = rootView.findViewById(R.id.swipe_refresh_lecturer);

    return rootView;
}

从代码中删除所有其他findViewByIds。