繁体   English   中英

如何在 Java 中使用包含视图的视图绑定?

[英]How to use View Binding with Included Views in Java?

View Binding 随 v3.6 发布。

https://developer.android.com/topic/libraries/view-binding

我将使用包含布局的视图绑定。

因此,我参考了以下有用的答案。

如何将视图绑定与包含的视图一起使用?

但效果不好。

MainActivity.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");
    }
}

activity_main.xml

<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,还有其他错误。

我已经解决了这个问题。

原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。

这就是我处理包含视图绑定的布局的方式。

// 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/

它现在已经有几年了,但仍然对您入门很有帮助。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM