房间持久性库(依赖性问题),MVVM设计模式

时间:2019-03-24 12:04:40

标签: android mvvm build.gradle android-room androidx

我遵循MVVM教程以及添加Room dependency

1-我在编译时遇到此错误:

Note: Failed to read get kotlin metadata for [Ljava.lang.Object;@54d771f9

Note: Failed to read get kotlin metadata for [Ljava.lang.Object;@4c00a268

Note: Failed to read get kotlin metadata for [Ljava.lang.Object;@42104314

Note: Failed to read get kotlin metadata for [Ljava.lang.Object;@203b72ad

2-也是:(已解决)

error: cannot find symbol class of

MainActivity中添加此代码时:

public class MainActivity extends AppCompatActivity {
private NoteViewModel noteViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    //error is here in the ViewModelProviders.of
    noteViewModel = new ViewModelProviders.of(this).get(NoteViewModel.class);
    noteViewModel.getAllNotes().observe(this, notes -> {
        //update RecyclerView
        Toast.makeText(MainActivity.this, "onChanged", Toast.LENGTH_SHORT).show();
    });
 }
}
  • build.gradle(项目):

    repositories {
     google()
     jcenter()
     maven { url "https://kotlin.bintray.com/kotlinx/" }
    }
    
    
    
    allprojects {
     repositories {
      google()
     jcenter()
     maven { url "https://kotlin.bintray.com/kotlinx/" }
     }
    }
    
  • build.gradle(app):

    room_version = "2.1.0-alpha06"
    
    implementation "androidx.room:room-runtime:$room_version"
    annotationProcessor "androidx.room:room-compiler:$room_version"
    

2 个答案:

答案 0 :(得分:1)

删除new, 这将起作用:

noteViewModel = ViewModelProviders.of(this).get(NoteViewModel.class);

答案 1 :(得分:0)

2.1.0-alpha04摆脱了这些错误,但是编译器在一些地方发出了有关“ RoomDatabase中的mCallbacks已被弃用”的警告。

我正在YouTube上使用完全相同的教程,并且遇到了相同的错误:“注意:无法读取get kotlin元数据...”错误。

我已经尝试过“ 2.1.0-alpha06”,“ 2.1.0-alpha05”版本的房间依赖项以及“ maven {url“ https://kotlin.bintray.com/kotlinx/”依赖项。...同样的错误。

希望能找到一个更好的答案,以清除这些错误。