gpt4 book ai didi

android - 如何从包含合并标签的布局访问 View ,该标签包含在另一个布局中?

转载 作者:行者123 更新时间:2023-12-03 13:26:18 25 4
gpt4 key购买 nike

我使用 Android Studio 3.6-RC1 和构建工具版本 3.6.0-rc01 并遇到了 ViewBinding 功能的问题:

我有带有以下标记的 activity_test.xml 文件:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<include
android:id="@+id/view_merged"
layout="@layout/merge_view" />
</LinearLayout>

并带有以下标记的 merge_view.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">

<TextView
android:id="@+id/label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Merge view" />
</merge>

Activity 代码如下所示:
class TestActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
val binding = ActivityTestBinding.inflate(layoutInflater)
setContentView(binding.root)

binding.viewMerged.label.text = "New text"

}
}

问题是当我尝试从合并布局访问 TextView 时,应用程序会抛出异常并显示消息 java.lang.NullPointerException: Missing required view with ID: viewMerged.
生成的绑定(bind)类如下所示:
public final class ActivityTestBinding implements ViewBinding {
@NonNull
private final LinearLayout rootView;

@NonNull
public final MergeViewBinding viewMerged;

private ActivityTestBinding(@NonNull LinearLayout rootView,
@NonNull MergeViewBinding viewMerged) {
this.rootView = rootView;
this.viewMerged = viewMerged;
}

@Override
@NonNull
public LinearLayout getRoot() {
return rootView;
}

@NonNull
public static ActivityTestBinding inflate(@NonNull LayoutInflater inflater) {
return inflate(inflater, null, false);
}

@NonNull
public static ActivityTestBinding inflate(@NonNull LayoutInflater inflater,
@Nullable ViewGroup parent, boolean attachToParent) {
View root = inflater.inflate(R.layout.activity_test, parent, false);
if (attachToParent) {
parent.addView(root);
}
return bind(root);
}

@NonNull
public static ActivityTestBinding bind(@NonNull View rootView) {
// The body of this method is generated in a way you would not otherwise write.
// This is done to optimize the compiled bytecode for size and performance.
String missingId;
missingId: {
View viewMerged = rootView.findViewById(R.id.view_merged);
if (viewMerged == null) {
missingId = "viewMerged";
break missingId;
}
MergeViewBinding viewMergedBinding = MergeViewBinding.bind(viewMerged);
return new ActivityTestBinding((LinearLayout) rootView, viewMergedBinding);
}
throw new NullPointerException("Missing required view with ID: ".concat(missingId));
}
}

我是否遗漏了什么,或者无法从包含的带有标签的布局中访问 View ,或者它尚未在 Android Studio 3.6-RC1 中提供?

最佳答案

我写了一篇关于使用 ViewBinding 和 <merge> 的文章您可以找到的标签 here

基本上,你需要做的是

  • 不给<include>标记任何 ID。
  • 调用 bind()生成合并布局绑定(bind)的方法,传递包含布局的布局的 Root View 。
  • 从合并绑定(bind)的对象访问您的 View

  • 例如,您有 merge_view.xml所以你会有 MergeViewBinding生成的类,这就是您从该布局访问 View 的方式。
    class TestActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding = ActivityTestBinding.inflate(layoutInflater)
    val mergeBinding = MergeViewBinding.bind(binding.root)
    setContentView(binding.root)
    mergeBinding.label.text = "New text"
    }
    }

    关于android - 如何从包含合并标签的布局访问 View ,该标签包含在另一个布局中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59753930/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com