NibbleBits
NibbleBits

Reputation: 124

fragment getView().findViewById returning null when changing orientation

For some strange reason when calling findViewById in the onStart method it sometimes returns null but only when rotating the screen.

public static class QuestionsFragment extends PlaceholderFragment {
    private RecyclerView questionsRecyclerView;
    private ConstraintLayout filtersLayout;
    private Button filtersBtn;
    private Spinner questionCategorySpinner;

    public QuestionsFragment() {

    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void onStart() {
        super.onStart();
        View view = getView();
        this.filtersBtn = (Button) getView().findViewById(R.id.FiltersBtn);
        this.filtersLayout = (ConstraintLayout) view.findViewById(R.id.filtersLayout);
        this.filtersBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                final Animation slideDownAnimation = AnimationUtils.loadAnimation(view.getContext(), R.anim.slidedown);
                final Animation slideUpAnimation = AnimationUtils.loadAnimation(view.getContext(), R.anim.slideup);
                boolean slideDown = filtersLayout.getVisibility() == View.GONE;
                filtersLayout.setVisibility(filtersLayout.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
                if (slideDown) {
                    filtersLayout.setAnimation(slideDownAnimation);
                } else {
                    filtersLayout.setAnimation(slideUpAnimation);
                }
            }
        });

        questionsRecyclerView = (RecyclerView) view.findViewById(R.id.questionsRecyclerView);
        questionCategorySpinner = (Spinner) view.findViewById(R.id.questionCategorySpinner);


        RecyclerView.LayoutManager questionsLayoutManager = new LinearLayoutManager(view.getContext());
        RecyclerView.Adapter questionsRecyclerViewAdapter = new QuestionAdapter(State.questions);
        questionsRecyclerView.setLayoutManager(questionsLayoutManager);
        questionsRecyclerView.setAdapter(questionsRecyclerViewAdapter);

        questionCategorySpinner.setAdapter(new ArrayAdapter<Category>(view.getContext(),
                android.R.layout.simple_spinner_dropdown_item, State.categories));
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_questions, container, false);
        return view;
    }
}

PlaceholderFragment extends Fragment.

I cannot seem to solve this issue I also cannot do this logic in the onCreate method as the onCreateView method would have not yet been called?

Can anyone help out here it would be greatly appreciated if anybody could tell me what is causing this problem. Stack trace

Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)' on a null object reference at com.mccarthydaniel.doask.MainActivity$QuestionsFragment.onStart(MainActivity.java:203) at android.support.v4.app.Fragment.performStart(Fragment.java:2380) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458) at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740) at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809) at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217) at android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3176) at android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:203) at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570) at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177) at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334) at android.app.Activity.performStart(Activity.java:7020)

Upvotes: 0

Views: 667

Answers (1)

NibbleBits
NibbleBits

Reputation: 124

Sorry guys the problem was stupid. I had a portrait layout and a landscape layout and in the landscape layout the view had a different id.

My bad.

Upvotes: 0

Related Questions