Android中的怪异数据绑定 [英] Weird DataBinding in Android

查看:86
本文介绍了Android中的怪异数据绑定的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图弄清楚如何在Android中使用数据绑定,但遇到了一个奇怪的问题.当我将ViewModel类放在某个包中时,生成的ActivityMainBinding无法看到它并说:error: package ViewModels does not exist.但是,如果我把它放在根包中,那么麻烦就消失了.是我的错误还是某种错误?

I try to figure out how to work with data binding in Android and met a strange problem. When I put my ViewModel class in some package, generated ActivityMainBinding can't see it and say: error: package ViewModels does not exist. But if I put it in the root package, then trouble disappears. Is it my mistake or some kind of bug?

代码:

activity_main.xml

activity_main.xml

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

<data>
    <variable
        name="DataContext"
        type="com.example.sombrero.bluem.ViewModels.MainViewModel" />
</data>
...

MainActivity.java

MainActivity.java

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    MainViewModel mainViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
    binding.setDataContext(mainViewModel);
}

MainViewModel目前只是一个空类.

MainViewModel is just empty class for now.

推荐答案

我也遇到了这个问题.感谢 tynn 回答我的

I ran into this as well. Thanks to tynn for answering my question.

根据tynn,这是因为我们没有遵循软件包的oracle命名约定.程序包名称应全部小写.将"ViewModels"更改为"viewmodels"为我修复了该问题.

According to tynn, it is because we didn't follow the oracle naming conventions for packages. The package name should be all lowercase. Changing "ViewModels" to "viewmodels" fixed it for me.

这篇关于Android中的怪异数据绑定的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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