[英]Android Data binding : Cannot resolve symbol
我曾嘗試在 android studio 中使用 beta 功能(數據綁定)。 按照android studio的指南,我可以在android studio中找到相關的類DataBindingInfo。 但是在我創建項目后不會生成數據綁定類。 有人可以幫忙嗎?
應用程序模塊的 build.gradle
apply plugin: 'com.android.application'
apply plugin: 'com.android.databinding'
android {
compileSdkVersion 23
buildToolsVersion "23.0.1"
defaultConfig {
applicationId "com.example.pigfamily.myapplication"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:23.0.1'
}
項目的 build.gradle
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
}
dependencies {
dependencies {
classpath "com.android.tools.build:gradle:1.3.0"
classpath "com.android.databinding:dataBinder:1.0-rc1"
}
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
jcenter()
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.example.pigfamily.myapplication.User" />
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.name}" />
</LinearLayout>
</layout>
MainActivity.java
package com.example.pigfamily.myapplication;
import android.databinding.DataBindingUtil;
import android.databinding.ViewDataBinding;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ActivityMainBinding //cannot resolve the symbol here
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
在您的App Level build.gradle 文件中啟用 dataBinding
android {
...
dataBinding{
enabled=true
}
}
我有同樣的問題。 我正在挖掘 gradle 設置,清理,重建......沒有任何效果。 最后我要做的就是重啟 Android Studio
https://www.bignerdranch.com/blog/descent-into-databinding/
在撰寫本文時,這種集成需要一點啟動才能開始。 要在添加標簽后使 ListItemCrimeBinding 可用,您必須重新啟動 Android Studio,然后重新構建項目。
要實現綁定,請確保:
在app.gradle
中,啟用dataBinding
:
apply plugin: 'com.android.application' ... android { ... dataBinding { enabled = true } ... } dependencies { ... }
將現有的 XML 布局包裝在<layout>
元素中:
<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"> <!-- note: xmlns above only needed if used in original XML --> <!-- original XML here --> </layout>
在Activity
/ Fragment
中,使用 Binding
val binding = XmlLayoutNameBinding.inflate(context.layoutInflater) // use the layout
if 無法解析XxxBinding
類(即使它可以解析XxxBindingImpl
)
Android Studio
,同步和重建項目添加 build.gradle(:app) viewBinding true
android {
buildFeatures {
viewBinding true
}
}
如果彈出對話框,請單擊同步,單擊保存旁邊的同步按鈕,或重新啟動 Android Studio。
如果clear Project
和Rebuild Project
對您不起作用,請嘗試簡單的解決方案。 當您從remote git branches
克隆項目時,它有時不起作用。您只需關閉您的項目並刪除您的 android 項目的.gradle
和.idea
文件夾。 然后重新打開項目。
對我來說,我必須補充:
buildFeatures {
viewBinding true
}
到我的build.gradle
文件,然后運行File->Sync Project With Gradle Files
就我而言,我的布局文件有一些錯誤,因此 AS 沒有創建綁定類。 我可以通過期待 XML 文件並查看錯誤消息來找到它。
確保在 build.gradle 中添加數據綁定
並在該同步項目之后重新啟動並從android studio
中的文件中清除緩存。 這種方式解決了我的問題
對於我的情況,我補充說:
dataBinding {
enabled=true
}
然后去 File > Settings > Gradle > Untick offline work > Apply > Ok 然后清理我的項目。
我在任何地方都沒有看到這個答案,所以我建議:如果您通過單擊“data”或“ui”之類的包從上下文菜單中添加了一些活動,Android Studio 會保存綁定路徑,如下所示:import com.damian.yourappname.ui .databinding.ActivitySearchBinding;
我在以前的項目中對其進行了幾次測試,並且僅在將諸如 ScrollingActivity 之類的示例活動添加到所有類的其他包的包中時才會發生這種情況。 導入 com.damian.yourappname.databinding.ActivitySearchBinding;
所以最好的方法是在根文件夾中創建示例活動,然后在 gradle 同步移動到某個包之后
我找到了一種無需重新啟動或使緩存無效的方法。 我之所以這樣做是因為綁定需要重新啟動什么的,所以我重新編寫了private ActivityMainBinding binding;
和 viewBinding 再次工作。
我不知道為什么會這樣,但這比重新啟動/無效緩存節省了我的時間
在我的情況下,我刪除了項目范圍內的 build、build-cache 文件夾,然后在 app 模塊中刪除了 build 文件夾。 之后重新啟動PC並開始工作
1.在app gradle下面添加
dataBinding {
enabled = true
}
2.在xml布局中寫下代碼
<layout
xmlns:android="http://schemas.android.com/apk/res/android">
<data></data>
</layout>
更新:清理並重建項目
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.