android.widget.TextView变为null?

时间:2018-06-22 22:30:26

标签: android android-lifecycle

在我的Fragment.onCreateView(inflater)中,我保留了对寡妇的引用:

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_shipping, container, false);
...
        mMyBtn = (Button) view.findViewById(R.id.mybtn);

所以我以后可以使用它:

    @Override
    public void onResume() {
        super.onResume();
...
        mMyBtn.setEnabled(false);

不知何故,我偶尔在Firebase Crashlytics中报告了mMyBtn.setEnabled中的nullpointer(100%Android 8设备)。

问题:我在错误的生命周期回调中做错了吗?即不是将小部件引用保留在 onCreateView(...)中,还是应该在 onViewCreated(...)中进行?我一直在做这种方式,一直在想我是否过时了……

有人建议让我的代码更具防御性吗?

0 个答案:

没有答案