找不到ActivityMainBindingImpl [英] ActivityMainBindingImpl cannot be found

查看:127
本文介绍了找不到ActivityMainBindingImpl的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是从Google网站上获得的:为每个布局文件生成一个绑定类.默认情况下,类的名称基于布局文件的名称,将其转换为Pascal大小写并向其添加Binding后缀.上面的布局文件名是activity_main.xml,因此相应的生成类是ActivityMainBinding.此类包含从布局属性(例如,用户变量)到布局视图的所有绑定,并且知道如何为绑定表达式分配值.

This is from the google site: A binding class is generated for each layout file. By default, the name of the class is based on the name of the layout file, converting it to Pascal case and adding the Binding suffix to it. The above layout filename is activity_main.xml so the corresponding generated class is ActivityMainBinding. This class holds all the bindings from the layout properties (for example, the user variable) to the layout's views and knows how to assign values for the binding expressions.

在我的情况下,会生成 ActivityMainBinding ,但不会生成 ActivityMainBindingImpl .那是什么班?它是如何产生的?我的项目是用Kotlin编写的.

In my case ActivityMainBinding is generated, but not ActivityMainBindingImpl. What is that class? How does it get generated? My project is written in Kotlin.

import android.util.SparseArray;
import android.util.SparseIntArray;
import android.view.View;
import androidx.databinding.DataBinderMapper;
import androidx.databinding.DataBindingComponent;
import androidx.databinding.ViewDataBinding;
import com.example.drake.kunuk.databinding.ActivityMainBindingImpl;
import java.lang.IllegalArgumentException;
import java.lang.Integer;
import java.lang.Object;
import java.lang.Override;
import java.lang.RuntimeException;
import java.lang.String;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class DataBinderMapperImpl extends DataBinderMapper {
  private static final int LAYOUT_ACTIVITYMAIN = 1;

  private static final SparseIntArray INTERNAL_LAYOUT_ID_LOOKUP = new SparseIntArray(1);

  static {
    INTERNAL_LAYOUT_ID_LOOKUP.put(com.example.drake.kunuk.R.layout.activity_main, LAYOUT_ACTIVITYMAIN);
  }

  @Override
  public ViewDataBinding getDataBinder(DataBindingComponent component, View view, int layoutId) {
    int localizedLayoutId = INTERNAL_LAYOUT_ID_LOOKUP.get(layoutId);
    if(localizedLayoutId > 0) {
      final Object tag = view.getTag();
      if(tag == null) {
        throw new RuntimeException("view must have a tag");
      }
      switch(localizedLayoutId) {
        case  LAYOUT_ACTIVITYMAIN: {
          if ("layout/activity_main_0".equals(tag)) {
            return new ActivityMainBindingImpl(component, view);
          }
      throw new IllegalArgumentException("The tag for activity_main is invalid. Received: " + tag);
    }
  }
}
return null;
  }

我的xml:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">

<data>
    <import type="android.view.View" />
    <variable
            name="handler"
            type="com.example.drake.kunuk.ui.main.MainActivity" />
    <variable
            name="manager"
            type="androidx.fragment.app.FragmentManager" />
</data>

<LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">

    <androidx.appcompat.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            app:title="@string/app_name"
            app:titleMarginStart="8dp" />

    <com.google.android.material.tabs.TabLayout
            android:id="@+id/tab_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:pager="@{(pager)}">
    </com.google.android.material.tabs.TabLayout>

    <androidx.viewpager.widget.ViewPager
            android:id="@+id/pager"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:handler="@{handler}" />

</LinearLayout>

MainActivity.kt:

MainActivity.kt:

import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import androidx.databinding.DataBindingUtil
import com.example.drake.kunuk.R
import com.example.drake.kunuk.databinding.ActivityMainBinding

class MainActivity : AppCompatActivity() {

override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    val binding: ActivityMainBinding = DataBindingUtil
        .setContentView(this, R.layout.activity_main)
    binding.handler = this
    binding.manager = supportFragmentManager



}

}

推荐答案

可能的原因是数据绑定阶段出现错误.

The likely cause is an error in the data binding stage.

数据绑定编译器获取布局文件并生成支持数据绑定的类(请注意: ActivityMainBinding ActivityMainBindingImpl ;一般模式,亲爱的读者是 { layout }绑定 { layout } BindingImpl ,其中 { layout }是布局文件的驼峰式名称).数据绑定编译期间出现的错误会阻止生成这些支持类.然后,这会导致您从Kotlin或Java编译器中看到缺少的类错误.

The data binding compiler takes layout files and generates classes to support data binding (as you note: ActivityMainBinding, ActivityMainBindingImpl; the general pattern, dear readers, is {layout}Binding and {layout}BindingImpl, where {layout} is the camel-cased name of the layout file). Errors that arise during data binding compilation prevent these support classes from being generated. This then causes the missing class error you see from the Kotlin or Java compiler.

当前,煮熟的构建日志中未显示数据绑定错误;要查看它们,请将视图切换到原始编译器输出.从AS 3.5开始,数据绑定错误应显示在煮熟的日志中.

Currently, data binding errors aren't shown in the cooked build log; to see them, switch the view to the raw compiler output. Starting around AS 3.5, data binding errors should be shown in the cooked log.

一旦找到了来自数据绑定编译器的错误消息,就可以对其进行修复,或者在不确定的情况下在此处寻找解决方案的答案.

Once you locate the error message from the data binding compiler, you can fix it, or look for an answer here on how to fix it if you're not sure.

这篇关于找不到ActivityMainBindingImpl的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆