我很遗憾地为我的android项目获取Room-Database的实例存在很大问题。我按照https://developer.android.com/training/data-storage/room/index.html上的说明进行操作 但最后一步对我不起作用:
AppDatabase db = Room.databaseBuilder(getApplicationContext(),
AppDatabase.class, "database-name").build();
他们的AppDatabase.java看起来像这样
@Database(entities = {User.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
public abstract UserDao userDao();
}
我的问题:在" AppDatabase db "我收到错误'无法解析符号" AppDatabase " &#39 ;.这非常有意义,因为AppDatabase只是一个抽象类。但这应该如何在教程中起作用? 我试图在MainActivity的onCreate方法中获取实例。 这是我的gradlefiles: buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.0.0'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
ext {
buildToolsVersion = "25.0.2"
supportLibVersion = "25.3.1"
archRoomVersion = "1.0.0-alpha1"
}
allprojects {
repositories {
google()
jcenter()
maven { url 'https://maven.google.com' }
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
和第二个:
apply plugin: 'com.android.application'
android {
compileSdkVersion 26
defaultConfig {
applicationId "com.example.fabian.kohlmannkonstruktionen"
minSdkVersion 15
targetSdkVersion 26
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:appcompat-v7:26.1.0'
implementation 'com.android.support.constraint:constraint-layout:1.0.2'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.1'
androidTestImplementation 'com.android.support.test.espresso:espresso-
core:3.0.1'
compile 'android.arch.persistence.room:runtime:' +
rootProject.archRoomVersion
annotationProcessor 'android.arch.persistence.room:compiler:' +
rootProject.archRoomVersion
}
MainActivity.java
package com.example.fabian.kohlmannkonstruktionen;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
AppDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = AppDatabase.getInMemoryDatabase(getApplicationContext());
}
...
AppDatabase.java
package com.example.fabian.kohlmannkonstruktionen;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.arch.persistence.db.SupportSQLiteDatabase;
import android.arch.persistence.room.Database;
import android.arch.persistence.room.Room;
import android.arch.persistence.room.RoomDatabase;
import android.arch.persistence.room.migration.Migration;
import android.content.Context;
public class MainActivity extends AppCompatActivity {
AppDatabase db;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
db = AppDatabase.getInMemoryDatabase(getApplicationContext());
}
Projekt.java
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.PrimaryKey;
/**
* Created by Fabian on 14.11.2017.
*/
@Entity
public class Projekt {
@PrimaryKey(autoGenerate = true)
Integer id;
Integer projektnummer;
Integer lohn,arbeitszeit;
String projektname, firma;
Boolean abgeschlossen;
public Projekt(String projektname,String firma,Integer projektnummer){
this.abgeschlossen=false;
this.projektname=projektname;
this.projektnummer=projektnummer;
this.firma=firma;
}
...getters and setters...
ProjektDao.java
import android.arch.persistence.room.Dao;
import android.arch.persistence.room.Delete;
import android.arch.persistence.room.Insert;
import android.arch.persistence.room.Query;
import java.util.List;
/**
* Created by Fabian on 15.11.2017.
*/
@Dao
public interface ProjektDao {
@Query("SELECT * FROM Projekt")
List<Projekt> getAll();
@Query("SELECT * FROM Projekt where id LIKE :id")
Projekt findByID(String id);
@Query("SELECT COUNT(*) from Projekt")
int countUsers();
@Insert
void insertAll(Projekt... projekts);
@Delete
void delete(Projekt projekt);
}
Termin和TerminDao看起来相同
这是gradle控制台的文字:
Executing tasks: [assemble]
Configuration on demand is an incubating feature.
Configuration 'compile' in project ':app' is deprecated. Use 'implementation' instead.
:app:compileDebugJavaWithJavac
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\TerminDao.java:14: warning: The query returns some columns [pausenzeit, startzeit, endzeit, lohnproh, mitarbeiter, standort] which are not use by Projekt. You can use @ColumnInfo annotation on the fields to specify the mapping. Projekt has some fields [projektnummer, lohn, arbeitszeit, projektname, firma, abgeschlossen] which are not returned by the query. If they are not supposed to be read from the result, you can mark them with @Ignore annotation. You can suppress this warning by annotating the method with @SuppressWarnings(RoomWarnings.CURSOR_MISMATCH). Columns returned by the query: id, pausenzeit, startzeit, endzeit, lohnproh, mitarbeiter, standort. Fields in Projekt: id, projektnummer, lohn, arbeitszeit, projektname, firma, abgeschlossen.
List<Projekt> getAll();
^
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\AppDatabase.java:13: warning: Schema export directory is not provided to the annotation processor so we cannot export the schema. You can either provide `room.schemaLocation` annotation processor argument OR set exportSchema to false.
public abstract class AppDatabase extends RoomDatabase {
^
C:\Users\Fabian\AndroidStudioProjects\KohlmannKonstruktionen\app\src\main\java\com\example\fabian\kohlmannkonstruktionen\MainActivity.java:13: error: cannot find symbol
AppDatabase db = AppDatabase.getInMemoryDatabase(getApplicationContext());
^
symbol: class AppDatabase
location: class MainActivity
1 error
2 warnings
FAILED
FAILURE: Build failed with an exception.
* What went wrong:
Execution failed for task ':app:compileDebugJavaWithJavac'.
> Compilation failed; see the compiler error output for details.
* Try:
Run with --stacktrace option to get the stack trace. Run with --info or --debug option to get more log output.
* Get more help at https://help.gradle.org
BUILD FAILED in 5s
14 actionable tasks: 1 executed, 13 up-to-date
答案 0 :(得分:4)
我的AppDatabase.class
与您的...
INSTANCE = Room.databaseBuilder(context, AppDatabase.class, "xxx")
// To simplify the codelab, allow queries on the main thread.
// Don't do this on a real app! See PersistenceBasicSample for an example.
.allowMainThreadQueries()
.build();
...
略有不同:
build.gradle
我的lifecycle
还包含// for room
compile 'android.arch.lifecycle:extensions:1.0.0';
compile 'android.arch.persistence.room:runtime:1.0.0';
annotationProcessor 'android.arch.lifecycle:compiler:1.0.0';
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0';
:
mmOutputStream.write(Mytext.getBytes(Charset.forName("UTF-8")));
答案 1 :(得分:3)
查看可能对您有帮助的代码。
AppDatabase.java
@Database(entities = {Product.class}, version = 1)
public abstract class AppDatabase extends RoomDatabase {
private static AppDatabase INSTANCE;
public abstract ProductDao productDao();
public static AppDatabase getInMemoryDatabase(Context context) {
if (INSTANCE == null) {
INSTANCE =
Room.inMemoryDatabaseBuilder(context.getApplicationContext(), AppDatabase.class).allowMainThreadQueries().build();
}
return INSTANCE;
}
public static void destroyInstance() {
INSTANCE = null;
}
}
Mainactivity.java
AppDatabase db = AppDatabase.getInMemoryDatabase(getApplicationContext());
的build.gradle(项目)
allprojects {
repositories {
jcenter()
maven { url 'https://maven.google.com' }
}
}
的build.gradle(APP)
// Room
compile 'android.arch.persistence.room:runtime:1.0.0'
annotationProcessor 'android.arch.persistence.room:compiler:1.0.0'
这将只创建一个实例。这在我的代码中效果很好。
答案 2 :(得分:0)
对我来说,这是复制/粘贴的情况。
重新输入@PrimaryKey等,以便Android Studio知道您正在使用它们,或者IDE只是将其视为文本