android 中的数据绑定对象可以为空吗
can data binding object be null in android
我正在我的项目中使用数据绑定。在一个片段中,我正在使用 DataBinding,如下所示
public class ExampleFragment extends Fragment {
private FragmentExampleBinding mFragmentExampleBinding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//initializing data binding object
mFragmentExampleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false);
// is there any posibility that mFragmentExampleBinding can be null
View view = mFragmentExampleBinding.getRoot();
return view;
}
}
我的问题是,数据绑定对象是否有可能 return 为空?我们真的需要在使用数据绑定时进行 null 检查吗?
一般情况下还是比较靠谱的,不用每次都查。
https://developer.android.com/reference/android/databinding/DataBindingUtil#inflate
* @return The newly-created binding for the inflated layout or <code>null</code> if
* the layoutId wasn't for a binding layout.
但如果xml中有错误,它可能为空。
但是,如果您决定清除 onDestroyView
中的视图引用(将绑定设置为空)以避免内存泄漏,则需要进行空检查或抛出异常,以防您在之后访问绑定视图被破坏。
至少现在,当使用一个 activity + 多个片段架构时,我必须将绑定(和其他视图引用)设置为 null 以避免内存泄漏。
我正在我的项目中使用数据绑定。在一个片段中,我正在使用 DataBinding,如下所示
public class ExampleFragment extends Fragment {
private FragmentExampleBinding mFragmentExampleBinding;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//initializing data binding object
mFragmentExampleBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_example, container, false);
// is there any posibility that mFragmentExampleBinding can be null
View view = mFragmentExampleBinding.getRoot();
return view;
}
}
我的问题是,数据绑定对象是否有可能 return 为空?我们真的需要在使用数据绑定时进行 null 检查吗?
一般情况下还是比较靠谱的,不用每次都查。
https://developer.android.com/reference/android/databinding/DataBindingUtil#inflate
* @return The newly-created binding for the inflated layout or <code>null</code> if * the layoutId wasn't for a binding layout.
但如果xml中有错误,它可能为空。
但是,如果您决定清除 onDestroyView
中的视图引用(将绑定设置为空)以避免内存泄漏,则需要进行空检查或抛出异常,以防您在之后访问绑定视图被破坏。
至少现在,当使用一个 activity + 多个片段架构时,我必须将绑定(和其他视图引用)设置为 null 以避免内存泄漏。