簡體   English   中英

無法訪問活動綁定android

[英]Cannot access activity binding android

我有一個名為activity_suggestions的布局文件。 我正在使用數據綁定。 因此生成了ActivitySuggestionsBinding文件。 該項目成功編譯。 但是當我嘗試運行項目時,我收到了這個錯誤

e: error: cannot access ActivitySuggestionsBinding

我使用的是android studio 3.1.2和kotlin 1.4.1版。 任何幫助將不勝感激

編輯
粘貼我的模塊級build.gradle和app level build.gradle

模塊Build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android {

    dataBinding {
        enabled = true
    }
..
}

dependencies{
..
    compile "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
    implementation "com.google.dagger:dagger:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android:$rootProject.daggerVersion"
    implementation "com.google.dagger:dagger-android-support:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    provided 'javax.annotation:jsr250-api:1.0'
    implementation "android.arch.lifecycle:runtime:$rootProject.archVersion"
    implementation "android.arch.lifecycle:extensions:$rootProject.archVersion"
    annotationProcessor "android.arch.lifecycle:compiler:$rootProject.archVersion"
    kapt "com.android.databinding:compiler:3.1.2"
..
}

App build.gradle

apply plugin: 'kotlin-android'
apply plugin: 'kotlin-kapt'

android{

    dataBinding{
        enabled = true
    }
..
}

dependencies{
    compile project(':module')
    kapt "com.google.dagger:dagger-android-processor:$rootProject.daggerVersion"
    kapt "com.google.dagger:dagger-compiler:$rootProject.daggerVersion"
    kapt "com.android.databinding:compiler:3.1.2"
..
}

這是我訪問ActivitySuggestionsBinding 這編譯沒有任何錯誤。

class SuggestionsActivityScreen : BaseActivity() {

    var binding : ActivitySuggestionsBinding? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        AndroidInjection.inject(this)
        super.onCreate(savedInstanceState)
        binding = DataBindingUtil.setContentView(this,R.layout.activity_suggestions)
        binding?.model = SuggestionActivityViewModel()

    }
}

在編譯基本模塊(app)時,這是我得到的錯誤

 error: cannot access ActivitySuggestionsBinding
  class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found
  Consult the following stack trace for details.
  com.sun.tools.javac.code.Symbol$CompletionFailure: class file for com.dom.comp.databinding.ActivitySuggestionsBinding not found

這是我的activity_suggestions.xml

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:card_view="http://schemas.android.com/apk/res-auto">
    <data>
        <variable
            name="model"
            type="com.dom.domp.SuggestionActivityViewModel"/>
    </data>

<RelativeLayout 
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:focusableInTouchMode="true"
    android:padding="@dimen/step1">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@{model.namedString}"/>
</RelativeLayout>
</layout>

我嘗試過干凈,無效的緩存。 這些都沒有解決問題。

在這個演示中它是工作並嘗試這種方式..只將數據綁定啟用代碼添加到應用程序級gradle文件,如下面的代碼..

dataBinding {
    enabled = true
}

下面的代碼是我的應用程序級gradle文件..

apply plugin: 'com.android.application'
apply plugin: 'kotlin-android'

android {
compileSdkVersion 27
defaultConfig {
    applicationId "com.example.adruser.rafdemo"
    minSdkVersion 23
    targetSdkVersion 27
    versionCode 1
    versionName "1.0"
    testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
dataBinding {
    enabled = true
}
}

dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:27.1.1'
implementation 'com.android.support.constraint:constraint-layout:1.1.0'
implementation 'com.android.support:support-v4:27.1.1'
implementation 'com.android.support:design:27.1.1'
testImplementation 'junit:junit:4.12'
implementation 'com.android.support:cardview-v7:27.1.1'
implementation 'com.google.android.gms:play-services-maps:11.6.0'
implementation 'com.google.android.gms:play-services-location:11.6.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'
implementation 'com.intuit.sdp:sdp-android:1.0.4'
implementation 'com.github.bumptech.glide:glide:4.7.1'


}
repositories {
mavenCentral()
}

然后在項目級別gradle.properties文件后添加以下行..

android.databinding.enableV2=true

make user_layout.xml文件如下代碼..

<?xml version="1.0" encoding="utf-8"?>
<layout
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
    <variable
        name="user"
        type="com.example.adruser.rafdemo.model.User"/>
</data>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text='@{user.name}'/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvName"
        android:text="@{user.dob}"/>

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvAge"
        android:text="@{Integer.toString(user.age)}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvName"
        />
    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="20dp"
        android:id="@+id/ulTvDob"
        android:text="@{user.dob ?? user.expDob}"
        app:layout_constraintTop_toBottomOf="@+id/ulTvAge"
        />
</android.support.constraint.ConstraintLayout>
</layout>

然后在制作User.java pojo類之后如下代碼..你也可以制作.kt類pojo

public class User {
public String name,dob,expDob;
public int age;
public User(){}
public User(String name, String dob,String expDob, int age) {
    this.name = name;
    this.dob = dob;
    this.age = age;
    this.expDob=expDob;
}
public static String display(){
    return "rajesh";
}
public String getName() {
    return name;
}

public void setName(String name) {
    this.name = name;
}

public String getDob() {
    return dob;
}

public void setDob(String dob) {
    this.dob = dob;
}

public int getAge() {
    return age;
}

public void setAge(int age) {
    this.age = age;
}

}

然后finnaly使DatabindingActivity.kt類進行綁定..

class DatabindingActivity :AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
    super.onCreate(savedInstanceState)
    var binding:UserLayoutBinding=DataBindingUtil.setContentView(this,R.layout.user_layout)
    val user = User("karan", null, "25/06/1994", 24)
    binding.user=user
}
}

app build.gradle添加以下app build.gradle

kapt "com.android.databinding:compiler:$android_plugin_version"
apply plugin: 'kotlin-kapt' // This one at top where plugin belong to

這樣就可以了。

$android_plugin_versionapplication build.gradlecom.android.tools.build:gradle版本

另外,將其添加到模塊build.gradle

android { /// Existing Code kapt { generateStubs = true } }

我有類似的問題。 我收到了錯誤

error: cannot access RepeatPaymentFragmentBinding
class file for RepeatPaymentFragmentBinding not found
Consult the following stack trace for details.
com.sun.tools.javac.code.Symbol$CompletionFailure: class file for my.package.databinding.RepeatPaymentFragmentBinding not found

花了幾天的時間才發現問題不在於GDB,而在於Dagger2。 事實證明,Dagger不支持在它生成的代碼中使用生成的類。

所以,我在我的:library模塊中有這樣的類:

class RepeatPaymentFragment: BaseFragment<RepeatPaymentFragmentBinding>() {
...
}

它的匕首組件位於:app模塊中。 因此兩個注釋處理器相互干擾。

從我的片段中刪除泛型后,它編譯得很好。

因此,如果您有這樣的錯誤,請嘗試查看是否嘗試注入(或提供)由另一個注釋進程生成的類,或者從此類繼承,或者具有此類的泛型。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM