片段getView()。findViewById在更改方向时返回null

时间:2017-11-20 20:35:52

标签: android android-fragments nullpointerexception

由于某些奇怪的原因,在onStart方法中调用findViewById时,它有时会返回null,但仅在旋转屏幕时才会显示。

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扩展了Fragment。

我似乎无法解决这个问题我也无法在onCreate方法中执行此逻辑,因为onCreateView方法尚未被调用?

任何人都可以在这里提供帮助,如果有人能告诉我导致这个问题的原因,我们将不胜感激。堆栈跟踪

  

引起:java.lang.NullPointerException:尝试在空对象引用上调用虚方法'void android.widget.Spinner.setAdapter(android.widget.SpinnerAdapter)'                                                                               at com.mccarthydaniel.doask.MainActivity $ QuestionsFragment.onStart(MainActivity.java:203)                                                                               在android.support.v4.app.Fragment.performStart(Fragment.java:2380)                                                                               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1458)                                                                               在android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)                                                                               在android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)                                                                               在android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3217)                                                                               在android.support.v4.app.FragmentManagerImpl.dispatchStart(FragmentManager.java:3176)                                                                               在android.support.v4.app.FragmentController.dispatchStart(FragmentController.java:203)                                                                               在android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:570)                                                                               在android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)                                                                               在android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1334)                                                                               在android.app.Activity.performStart(Activity.java:7020)

1 个答案:

答案 0 :(得分:0)

对不起,伙计们这个问题很愚蠢。我有一个纵向布局和一个横向布局,在横向布局中,视图有不同的ID。

我的坏。