我有两个带注释的ActiveAndroid模型类,术语和课程(一对多关系)。
我能够创建Term和Course对象,并在其上调用.save()而不会出现任何错误。
但是,当我尝试在直接访问其成员之外查询对象时,收到错误消息:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.gilbertdev.wgu.c196.abm1">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<meta-data
android:name="AA_MODELS"
android:value="com.gilbertdev.wgu.c196.abm1.Term, com.gilbertdev.wgu.c196.abm1.Course" />
<meta-data android:name="AA_DB_VERSION" android:value="10" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
或者如果我尝试查询课程,则类似。
据我所知,我尚未更改架构。
我尝试过的事情: 1.在模拟器中卸载并重新安装应用程序。 2.在Android清单中更改AA_DB_VERSION。
清单:
dependencies {
implementation fileTree(include: ['*.jar'], dir: 'libs')
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
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 files('libs/activeandroid-3.0.jar')
}
依赖项:
package com.gilbertdev.wgu.c196.abm1;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import java.util.Date;
@Table(name = "Terms")
public class Term extends Model {
@Column(name = "title")
public String title;
@Column(name = "startdate")
public Date startdate;
@Column(name = "enddate")
public Date enddate;
//@Column(name = "course")
//public Course course;
public Term () {
super();
}
}
Term.java
package com.gilbertdev.wgu.c196.abm1;
import com.activeandroid.Model;
import com.activeandroid.annotation.Column;
import com.activeandroid.annotation.Table;
import java.util.Date;
@Table(name = "Courses")
public class Course extends Model {
@Column(name = "title")
public String title;
@Column(name = "startdate")
public Date startdate;
@Column(name = "enddate")
public Date enddate;
@Column(name = "status")
public String status;
@Column(name = "term")
public Term term;
public Course() {
super();
}
}
Course.java
package com.gilbertdev.wgu.c196.abm1;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.TextView;
import com.activeandroid.ActiveAndroid;
import com.activeandroid.Model;
import com.activeandroid.query.Select;
import java.util.Date;
import java.util.List;
public class MainActivity extends AppCompatActivity {
TextView testView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActiveAndroid.initialize(this);
setContentView(R.layout.activity_main);
testView = (TextView) findViewById(R.id.testtext);
testView.append("\nGilbert\n");
Term firstterm = new Term();
firstterm.title = "First term";
firstterm.startdate = new Date(1991, 2, 20);
firstterm.enddate = new Date(2100, 2, 20);
firstterm.save();
Course firstcourse = new Course();
firstcourse.title = "First course";
firstcourse.startdate = new Date(2018, 1, 13);
firstcourse.enddate = new Date(2018, 1, 20);
firstcourse.status = "In Progress";
firstcourse.term = firstterm;
firstcourse.save();
testView.append(firstterm.title + "\n");
testView.append(firstterm.startdate + "\n");
testView.append(firstcourse.term.title + "\n");
try {
List<Course> courses = new Select()
.from(Course.class)
.where("Term = ?", firstterm.getId())
.orderBy("title ASC")
.execute();
for (Course course : courses) {
testView.append(course.status);
}
// Term test = new Select().from(Term.class).orderBy("RANDOM()").executeSingle();
// testView.append(test.title);
} catch (Exception e) {
testView.append(e.getMessage());
}
}
}
MainActivity.java
function Test-Upload(){
Param()
Process{
$data=@"
{
"fields":
{
"project":
{
"key": "CCWASSET"
},
"summary": "Testing Linked Field",
"issuetype":
{
"name": "Asset"
},
"description" : "Testing Linked Field"
},
"update":{
"customfield_10500":[
{
"set":{
"type":{
"name":"Asset PO",
"inward":"Asset",
"outward":"Purchase Order"
},
"outwardIssue":{
"key":""
}
}
}
]
}
}
"@
return Jira-WebRequest -data $data
}
}
function Jira-WebRequest(){
Param(
[Parameter(mandatory=$false)]$data,
[Parameter(mandatory=$false)]$requesttype="issue",
[Parameter(mandatory=$false)]$method="POST",
[Parameter(mandatory=$false)]$ContentType='application/json'
)
Process{
$path = $("/rest/api/2/$requesttype/")
$Uri = ""
[URI]::TryCreate([URI]::new($Settings.Jira.URL),"$path",$([ref]$Uri))
$Params = @{
ContentType = $ContentType
Body = $data #$(@{"vlan_id"=$vlanID;"port_id"="$portID";"port_mode"="$portMode"} | ConvertTo-JSON)
Method = $method
URI = $uri.AbsoluteUri
Headers = $JiraHeaders
#WebSession = $Session
}
try{
$result = Invoke-RestMethod @Params -Verbose
return $result
} Catch [System.Net.WebException] {
$exception = $_.Exception
$respstream = $exception.Response.GetResponseStream()
$sr = new-object System.IO.StreamReader $respstream
$ErrorResult = $sr.ReadToEnd()
return $ErrorResult
}
}
}
答案 0 :(得分:1)
我尝试了另一个ORM并遇到了同样的问题,所以我发现发生了一些不适用于ActiveAndroid的奇怪事件。我使用Android Studio中的设备文件资源管理器复制了两个ORM创建的SQLite DB文件,打开它们,然后看到没有创建任何表,并且在调用.save()后看不到任何数据可持久化
如何修复:
在Mac上,导航至Android Studio->首选项->构建,执行,部署->即时运行,然后取消选中启用即时运行。
您可以在Android Studio中执行以下操作,方法是依次导航到“视图”->“工具窗口”->“设备文件资源管理器”,然后选择“ data / data / com.yoursite.yourapp / databases”,然后删除所有相关的.db文件。