View Binding随v3.6一起发布。
https://developer.android.com/topic/libraries/view-binding
我将在包含的布局中使用视图绑定。
因此,我参考以下有用的答案。
How to use View Binding with Included Views?
但是效果不好。
当然,我试图这样用Java编写。
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding binding = ActivityMainBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
mainBinding.myHeader.foo.setText("this is a test");
}
}
主布局包括另一个布局
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="@+id/my_header"
layout="@layout/item_header"
android:layout_width="match_parent"
android:layout_height="100dp" />
</LinearLayout>
但是,发生以下错误。
2020-04-21 10:18:25.135 30977-30977/com.example.myapplication E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.myapplication, PID: 16941
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.myapplication/com.example.myapplication.MainActivity}: java.lang.NullPointerException: Missing required view with ID: itemId
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3270)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
Caused by: java.lang.NullPointerException: Missing required view with ID: itemId
at com.example.myapplication.databinding.ItemHeaderBinding.bind(ItemHeaderBinding.java:73)
at com.example.myapplication.databinding.ActivityMainBinding.bind(ActivityMainBinding.java:60)
at com.example.myapplication.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:46)
at com.example.myapplication.databinding.ActivityMainBinding.inflate(ActivityMainBinding.java:36)
at com.example.myapplication.MainActivity.onCreate(MainActivity.java:13)
at android.app.Activity.performCreate(Activity.java:7825)
at android.app.Activity.performCreate(Activity.java:7814)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1306)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:3245)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:3409)
at android.app.servertransaction.LaunchActivityItem.execute(LaunchActivityItem.java:83)
at android.app.servertransaction.TransactionExecutor.executeCallbacks(TransactionExecutor.java:135)
at android.app.servertransaction.TransactionExecutor.execute(TransactionExecutor.java:95)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2016)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:214)
at android.app.ActivityThread.main(ActivityThread.java:7356)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:492)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:930)
怎么了?
如果我删除包含布局的ID,则还有其他错误。
答案 0 :(得分:1)
我已经解决了问题。
原始item_header.xml
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/item_id"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:background="@android:color/transparent">
<TextView
android:id="@+id/foo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
我删除了framelayout的ID,错误很快消失了。
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:background="@android:color/transparent">
<TextView
android:id="@+id/foo"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</FrameLayout>
在使用视图绑定时请小心使用id。
答案 1 :(得分:0)
这就是我处理包含视图绑定的包含布局的方式。
// When view binding is enabled it will create a binding class for each layout, converting the file name to Pascal case.
ActivityMainBinding binding;
IncludedLayout bindingIncludedLayout;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Inflate the layout as per google doc instructions
binding = ActivityMainBinding.inflate(getLayoutInflater());
// add the inflated view to the Included view.
bindingIncludedLayout = binding;
setContentView(binding.getRoot());
// binding in the main layout
binding.myView.setText("text");
// binding in the included layout
bindingIncludedLayout.textview_name.("additional text")
}
我强烈建议通过视图绑定学习数据绑定,因为数据绑定具有更大的潜力,就像类固醇上的视图绑定一样。
这是我遇到的学习数据绑定的最佳教程。
https://www.androidhive.info/android-working-with-databinding/
它已经有几年历史了,但是对于您入门还是很有帮助的。