我不明白为什么for循环会破坏我的应用程序。我知道这是一个简单的问题,但我一直盯着这个代码数小时试图弄清楚。我相当肯定build.gradle中的所有依赖项都是正确的。如果你需要更多的信息,请告诉我。谢谢。
//应用程序类
package edu.uncw.seahawktours;
import android.app.Application;
import java.util.ArrayList;
import java.util.List;
import io.objectbox.Box;
import io.objectbox.BoxStore;
public class App extends Application {
private BoxStore boxStore;
@Override
public void onCreate() {
super.onCreate();
// Initialize the main data access object
boxStore = MyObjectBox.builder().androidContext(App.this).build();
// Get the wrapper (Box) for the Book table that lets us store Book objects
Box<Building> buildingBox = boxStore.boxFor(Building.class);
// Initialize with some data
if (buildingBox.count() == 0) {
List<Building> initialBooks = new ArrayList<>();
initialBooks.add(new Building("CIS", "https://library.uncw.edu/web/collections/archives/bnl/cis.html", R.drawable.cis));
initialBooks.add(new Building("Depaolo Hall", "https://library.uncw.edu/web/collections/archives/bnl/6.html", R.drawable.depaolo));
initialBooks.add(new Building("Trask Coliseum", "https://library.uncw.edu/web/collections/archives/bnl/10.html", R.drawable.trask));
initialBooks.add(new Building("King Hall", "https://library.uncw.edu/web/collections/archives/bnl/4.html", R.drawable.kinghall2));
initialBooks.add(new Building("Leutze Hall", "https://library.uncw.edu/web/collections/archives/bnl/17.html", R.drawable.leutzehall));
// ObjectBox is smart enough to handle CRUD on Collections of entities
buildingBox.put(initialBooks);
}
System.out.println(buildingBox.count());
for (Building book : buildingBox.getAll()) {
System.out.println(book.getBuildingName());
}
}
public BoxStore getBoxStore() {
return boxStore;
}
}//建筑班
package edu.uncw.seahawktours;
import io.objectbox.annotation.Entity;
import io.objectbox.annotation.Id;
@Entity
public class Building {
@Id public long id;
private String buildingName;
private String description;
private String url;
private int imageID;
public Building(String name, String url ,int buildingPictureID){
this.buildingName = name;
this.url = url;
this.imageID = buildingPictureID;
}
public long getId() {
return id;
}
public String getDescription() {
return description;
}
public String getBuildingName() {
return buildingName;
}
public String getUrl() {
return url;
}
public int getImageID() {
return imageID;
}
public void setId(long id) {
this.id = id;
}
public void setBuildingName(String buildingName) {
this.buildingName = buildingName;
}
public void setDescription(String description) {
this.description = description;
}
public void setUrl(String url) {
this.url = url;
}
public void setImageID(int imageID) {
this.imageID = imageID;
}
@Override
public String toString() {
return this.buildingName;
}
}// Build.Gradle项目
// Top-level build file where you can add configuration options common to
all sub-projects/modules.
buildscript {
ext.objectboxVersion = '2.2.0'
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.0'
classpath "io.objectbox:objectbox-gradle-plugin:$objectboxVersion"
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}// App Build.Gradle
apply plugin: 'com.android.application'
apply plugin: 'io.objectbox'
android {
compileSdkVersion 28
defaultConfig {
applicationId "edu.uncw.seahawktours"
minSdkVersion 19
targetSdkVersion 28
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(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation 'com.android.support:design:28.0.0'
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'
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support:recyclerview-v7:28.0.0'
implementation 'com.android.support:cardview-v7:28.0.0'
}发布于 2019-02-11 21:53:31
事实证明,在安卓开发中使用ObjectBox时,有一个空的构造函数是至关重要的。这可能与你的回答有关是一种感觉,但这就是修正程序的原因。在我看来是很愚蠢的执行。
发布于 2019-02-09 08:32:47
我的回答是一个糟糕的答案,但是ObjectBox只对“平面”数据结构有好处。关系实现是可怕的,和RealmDb一样糟糕。为什么我们不能嵌入对象,为什么一切都是关系?还有一种更慢的选择,Couchbase Mobile。几乎没有人提到它,但它更像是手机上的MongoDb。我以前试过,不喜欢它-我仍然不喜欢它,但我不需要花费数天的时间去寻找文件来使它工作。你只要保存你的数据结构。它不像ObjectBox那样的打字(这很糟糕)。似乎所有的产品在不同的层次上都很糟糕。
因此,请考虑一下:
我知道这没有帮助,投我反对票,但我花了一段时间玩所有这些,仍然没有一个明确的路径离线存储为基础的应用程序。
https://stackoverflow.com/questions/53567196
复制相似问题