DataBindingUtil.setContentView - 类型参数T具有不兼容的上限

时间:2015-08-23 19:24:03

标签: android

“Android Studio”显示错误消息“类型参数T具有不兼容的上限:ViewDataBinding和ActivityChecklistsBinding。

ActivityChecklistsBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_checklists);

在更新ADK之前,它可以正常工作。有什么想法有什么不对吗?

9 个答案:

答案 0 :(得分:73)

我遇到了同样的问题。我尝试了一些清洁和重建项目。

但是,在我选择File -> Invalidate Caches / Restart

之后,它起了作用

答案 1 :(得分:2)

自动生成绑定活动并从布局文件中获取名称,而不是活动类。

因此,如果您有一个名为BeautifulActivity的活动和名为sweet_layout.xml的相应布局,则生成的名称将为SweetLayoutBinding而不是BeautifulActivityBinding

MainActivityactivity_main.xml

之间混淆不要犯同样的错误

来源Android Developers

答案 2 :(得分:1)

当我第一次遇到这个错误时,我创建了一个名为a.xml的布局,然后我创建了一个这样的活动

public class ABinding extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ABinding binding = DataBindingUtil.setContentView(this, R.layout.a);
    }

}

发生此错误。 最后我发现Class ABinding是在/ build中自动生成的,所以名为ABinding的活动将覆盖自动生成的类

所以我重命名活动,错误消失

答案 3 :(得分:1)

添加这是您的build.gradle(Module:app)文件

android {

dataBinding {
    enabled true
}

}

答案 4 :(得分:1)

Go to layout "main_activity.xml"
Click right -> Refactor -> Rename
Rename the layout to "main_activity_new.xml" or anything you want
Again rename it to normal "main_activity.xml"

对我来说固定了!

答案 5 :(得分:0)

将此添加到build.gradle

   android{
 ....
    dataBinding 
      { 
     enabled = true
       }
...
    }

答案 6 :(得分:0)

如果上述所有解决方案均无法解决,请转到您的 .gradle文件夹>缓存,然后删除所有名称以 transforms 开头的文件夹,即transforms-1&转换2。这对我有用

答案 7 :(得分:0)

Try This Work for sure...

  Step 1: Add this code in the build.gradle(Mobile:app)
           dataBinding {
                enabled = true
            }

        Example:
         buildTypes {
                release {
                    minifyEnabled false
                    proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
                }
            }

            **dataBinding {
                enabled = true
            }**
        }

        dependencies {
        implementation fileTree(dir: 'libs', include: ['*.jar'])
            implementation 'com.android.support:appcompat-v7:28.0.0'
            implementation 'com.android.support.constraint:constraint-layout:1.1.3'
            testImplementation 'junit:junit:4.12'
        .....
        }

        Step 2:
        Binding Can be done with Name of the .xml file as below example..
        Simply name of the xml file and prefix with binding...

        Example 1:
        if of your .xml file is activity_main.xml then Binding file should be MainActivityBinding
        Example 2:
        if of your .xml file is android_sunil.xml then Binding file should be AndroidSunilBinding

    Step 3: Sample Code:

    public class BaseObservableActivity extends AppCompatActivity {

            private ActivityBaseobservableBinding activityMainBinding;
        @Override
        protected void onCreate(@Nullable Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_baseobservable);

        }
    }

In the above code my .xml file name is activity_baseobservable so my binding class should be ActivityBaseobservableBinding activityMainBinding


--Happy Android Coding@Ambilpura

答案 8 :(得分:-1)

您应该使用 ActivityMainBinding 代替您的Activity类名。

ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_checklists);