我在当前项目中使用 Butterknife 库时遇到一些问题。我目前正在针对手机和平板电脑优化该项目,例如,两者的布局文件有时会存在细微差别
布局/layout_example.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text1/>
</LinearLayout>
布局大/layout_example.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="@+id/text1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text1/>
<TextView
android:id="@+id/text2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text2/>
</LinearLayout>
该类看起来像这样:
@Bind(R.id.text1) TextView text1;
@Bind(R.id.text2) TextView text2;
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.layout_example, container, false)
ButterKnife.bind(this, rootView);
return rootView;
}
问题是,由于“text2”视图不存在,因此在手机上查看时会引发缺少视图的异常。有没有办法解决?或者我是否必须对并非在所有布局上可用的视图使用 findViewById 。谢谢!
在可能为空的视图的@Bind之前添加注释@Nullable。
示例来自黄油刀网站 http://jakewharton.github.io/butterknife/:
@Nullable @Bind(R.id.might_not_be_there) TextView mightNotBeThere;
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)