【问题标题】:How to use View Binding with Included Views in Java?如何在 Java 中将视图绑定与包含的视图一起使用?
【发布时间】:2020-04-21 02:43:22
【问题描述】:

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

【问题讨论】:

标签: android view binding


【解决方案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。

【讨论】:

    【解决方案2】:

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

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-09-07
      • 2020-11-21
      • 2020-02-21
      • 2013-12-26
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多