android–Butterknife与手机和平板电脑的不同布局

android–Butterknife与手机和平板电脑的不同布局,第1张

概述我在当前项目中使用Butterknife库时遇到了一些问题.我目前正在优化手机和平板电脑的项目,例如,两者的布局文件之间有时会略有不同布局/layout_example.xml<LinearLayoutxmlns:android="http://schemas.android.com/apkes/android"android:layout_width="match_parent"

我在当前项目中使用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可能为null.

ButterKnife Website的示例:

@Nullable @Bind(R.ID.might_not_be_there) TextVIEw mightNotBeThere;
总结

以上是内存溢出为你收集整理的android – Butterknife与手机和平板电脑的不同布局全部内容,希望文章能够帮你解决android – Butterknife与手机和平板电脑的不同布局所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1108137.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-29
下一篇2022-05-29

发表评论

登录后才能评论

评论列表(0条)

    保存