数据绑定“错误:找不到符号类模型". [英] Data binding "error: cannot find symbol class Models"

查看:78
本文介绍了数据绑定“错误:找不到符号类模型".的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我需要确认明显相似的但不能重复问题

First, I need to acknowledge the clearly very similar but not duplicate issue here. None of the proposed solutions in that thread work.

我的应用程序文件结构如下:

My application file structure is as follows:

app
  java
    [mydomain].[myapplication]
      Models
        DataModel.java
      MainActivity.java
  res
    layout
      activity_main.xml
      content_main.xml
      my_inner_layout.xml

我的应用程序build.gradle包含

dataBinding {
    enabled = true
}

MainActivity.java我有

import [mydomain].[myapplication].Models.DataModel;

public class MainActivity extends AppCompatActivity
        implements NavigationView.OnNavigationItemsSelectedListener {

    DataModel dataModel;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        ... <other layout creation template code> ...

        ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        dataModel = new DataModel();
        binding.setValues(dataModel);
    }

    <navigation and auto-generated methods>
}

我的my_inner_layout.xml包含

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
    <data>
        <variable
            name="values"
            type="[mydomain].[myapplication].Models.DataModel" />
    </data>
    <android.support.constraint.ConstraintLayout
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:visibility="visible"
        >

        <TextView
            android:id="@+id/intro_text"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="20dp"
            android:text="@{values.valueOne}"/>

        <TextView
            android:id="@+id/buying_recommendation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_below="@id/intro_text"
            app:layout_constraintTop_toBottomOf="@id/intro_text"
            android:text="@{values.valueTwo}"/>
    </android.support.constraint.ConstraintLayout>
</layout>

我正在将bind:values="@{values}"从activity_main传递到其include d app_bar_main,并将它的content_main传递到其my_inner_layout,并且每个中都具有相同的<data>值. Android Studio告诉我命名空间'bind'未绑定".

I am passing bind:values="@{values}" through from activity_main to its included app_bar_main to its content_main to its my_inner_layout with that same <data> value in each. Android Studio is telling me "namespace 'bind' is not bound".

如果尝试运行此命令,则会收到编译失败;有关详细信息,请参见编译器错误输出".在构建输出中,我看到:

If I try to run this, I get "Compilation failed; see the compiler error output for details." Looking in the build output, I see:

在文本中,错误有不同的error: cannot find symbol class Modelserror: package Models does not exist

In text, the errors are variously error: cannot find symbol class Models and error: package Models does not exist

如果我将DataModel.java从Models包中移出并直接移到[mydomain].[myapplication]中,那么我会得到不同的结果.它可以在模拟器中构建并运行,但是许多布局信息无法显示.左上角没有汉堡菜单,标题中没有标题文本,右上角没有设置按钮,以前由Android Studio中自动生成的代码自动包含的值.我也无法使用setTitle在代码中设置标题.

If I move DataModel.java out of the Models package and directly in to [mydomain].[myapplication], then I get a different result. It does build and run in the emulator, but with much of the layout information failing to appear. No hamburger menu in the top left, no title text in the header, and no settings button in the top right values previously automatically included by the autogenerated code in Android Studio. I am unable to set the title in code using setTitle, either.

从左侧滑动确实会带来导航抽屉.

Swiping from the left does bring in the navigation drawer however.

我尝试过使缓存无效,并分别使用模型"中的模型文件以及分别重新启动,清理和重建.

I have tried invalidating caches and restarting, cleaning, rebuilding both with the model file in Models and separately.

首先,我想要的是能够使用我想要的项目结构.将我的模型类放在模型子包中.完成后,我想确保完整的布局信息通过,包括汉堡菜单图标,设置图标和标题.我该如何实现?

What I want, chiefly, is to be able to use the project structure I want. To put my models classes in a models sub-package. Once that is complete, I want to make sure the full layout information comes through, including the hamburger menu icon, settings icon, and title. How can I achieve this?

推荐答案

好吧,我意识到类模型不存在"的来源.我不知道是怪我自己的愚蠢还是怪异的挑剔方式在Android上实现了这种绑定.必须使用小写的"m"而不是Models来调用该软件包.绑定自动名称转换的东西一定以为Models是一个类,而不是一个包.

Okay, I realised where the "class Models does not exist" thing comes from. I don't know whether to blame my own stupidity or the stupidly nitpicky way this binding is implemented on Android. The package needed to be called models with a lower case "m", not Models. The binding auto-name-conversion thing must have thought Models was a class, not a package.

要修复布局,必须将onCreate方法更改为

To fix the layout, the onCreate method had to be changed to

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.activity_main);

    ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    dataModel = new DataModel();
    cycleInformationBinding.setRecommendation(dataModel);

    // set toolbar
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    // Drawer layout setting
    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();

    NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
    navigationView.setNavigationItemSelectedListener(this);
}

具体来说,事情必须按以下顺序发生:

Specifically, things had to happen in the order:

  1. setContentView转到主要活动
  2. 设置数据模型绑定
  3. 有关布局的问题,例如抽屉和工具栏.
  1. setContentView to the main activity
  2. Set up the data model binding
  3. Layout concerns like drawer and toolbar.

任何其他顺序都会导致模型绑定失败或工具栏无法正确显示.

Any other order would cause either the model binding to fail or the toolbar to not display correctly.

这篇关于数据绑定“错误:找不到符号类模型".的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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