编程语言
首页 > 编程语言> > Android:程序类型已存在:android.arch.lifecycle.LiveData $LifecycleBoundObserver

Android:程序类型已存在:android.arch.lifecycle.LiveData $LifecycleBoundObserver

作者:互联网

我看了一些答案但与firebase和实时数据不相容或类似相关.我的简单测试应用程序与firebase无关.

这是模块级别的gradle文件:

    apply plugin: 'com.android.application'

android {
    compileSdkVersion 27
    defaultConfig {
        applicationId "room.exercise.com.myapplication"
        minSdkVersion 19
        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'
        }
    }
}

dependencies {
    implementation fileTree(dir: 'libs', include: ['*.jar'])

    implementation 'com.android.support:recyclerview-v7:27.1.1'

    //Design for room persistance storage
    implementation 'android.arch.persistence.room:runtime:1.0.0'
    annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
    /////

    //Design for live data
    implementation "android.arch.lifecycle:extensions:1.0.0"
    annotationProcessor "android.arch.lifecycle:compiler:1.0.0"
    //

    implementation 'com.android.support:appcompat-v7:27.1.1'
    implementation 'com.android.support.constraint:constraint-layout:1.1.2'
    testImplementation 'junit:junit:4.12'
    androidTestImplementation 'com.android.support.test:runner:1.0.2'
    androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}

这个应用程序只是对Room和Live数据的简单测试,我将数据从数据库加载到recyclerView ..我知道Room数据库正在工作,这是我的第一个测试.

gradle文件同步没有问题.编译并运行应用程序时发生错误.

Program type already present: android.arch.lifecycle.LiveData$LifecycleBoundObserver
Message{kind=ERROR, text=Program type already present: android.arch.lifecycle.LiveData$LifecycleBoundObserver, sources=[Unknown source file], tool name=Optional.of(D8)}

码:

public class RoomActivity extends AppCompatActivity implements View.OnLongClickListener {

private StudentListViewModel viewModel;

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

    setContentView(R.layout.activity_room);

    //RoomDatabase database = RoomDatabase.getDatabase(RoomActivity.this);
    //RoomDataInitializer.AddSampleDataAsync(database);

    //List<StudentEntity> students = database.studentDAO().getAllStudents();
    //Log.w("Room Activity", "Count = " + students.size());

    RecyclerView recyclerView = (RecyclerView)findViewById(R.id.recycler_view);
    final RecyclerViewAdapter adapter = new RecyclerViewAdapter(new ArrayList<StudentEntity>(), this);
    recyclerView.setAdapter(adapter);

    viewModel = ViewModelProviders.of(this).get(StudentListViewModel.class);

    viewModel.getStudentList().observe(RoomActivity.this, new Observer<List<StudentEntity>>()
    {
        @Override
        public void onChanged(@Nullable List<StudentEntity> studentEntities) {
            adapter.addItems(studentEntities);
        }
    });

}

@Override
public boolean onLongClick(View v) {
    return false;
}

}

解决方法:

试试这个:

implementation "android.arch.lifecycle:extensions:1.1.1"
annotationProcessor "android.arch.lifecycle:compiler:1.1.1"

implementation 'android.arch.persistence.room:runtime:1.1.1'
annotationProcessor 'android.arch.persistence.room:compiler:1.1.1'

而不是你的版本.

我对此解决方案没有可解释的解释,但我会尝试.

当一个类由2个或更多依赖项定义/引入时,会发生此类错误. (在您的情况下,它是LifecycleBoundObserver).

而这种类型的错误只发生在运行时.在gradle构建/同步期间不会发生这些问题.

如果在firebase / android依赖项中发生此类错误,通常可以通过升级版本来解决它们. (或者在问题跟踪器中可能存在相同的问题.).在其他情况下,您可能必须从引入冗余类的依赖项中排除模块/组.

标签:android,android-recyclerview,android-livedata
来源: https://codeday.me/bug/20190731/1586964.html