在下面的Realm示例中,我创建了两个模型.一个叫做Task,另一个叫做User.在Realm的帮助下,我将使用一对一的关系.即.用户"Theo"有一个任务.
所以这是我的模特.
@RealmClass
public class Task implements RealmModel {
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
@PrimaryKey
private String id;
private String title;
public boolean isCompleted() {
return isCompleted;
}
public void setCompleted(boolean completed) {
isCompleted = completed;
} …Run Code Online (Sandbox Code Playgroud) 运行我的应用程序时出现此异常。我该如何解决?
E/AndroidRuntime: FATAL EXCEPTION: main
Process: theo.testing.realmapplication, PID: 3359
java.lang.RuntimeException: Unable to start activity ComponentInfo{theo.testing.realmapplication/theo.testing.realmapplication.MainActivity}: java.lang.IllegalStateException: The Realm is already in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 113
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2416)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2476)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1344)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5417)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)
Caused by: java.lang.IllegalStateException: The Realm is already in a write transaction in /Users/cm/Realm/realm-java/realm/realm-library/src/main/cpp/io_realm_internal_SharedRealm.cpp line 113
at io.realm.internal.SharedRealm.nativeBeginTransaction(Native Method)
at io.realm.internal.SharedRealm.beginTransaction(SharedRealm.java:219)
at io.realm.BaseRealm.beginTransaction(BaseRealm.java:328)
at io.realm.Realm.beginTransaction(Realm.java:125)
at theo.testing.realmapplication.MainFragment$1.execute(MainFragment.java:47)
at io.realm.Realm.executeTransaction(Realm.java:1253)
at theo.testing.realmapplication.MainFragment.onActivityCreated(MainFragment.java:43)
at …Run Code Online (Sandbox Code Playgroud) 我有以下代码从openweathermap api中检索一些天气数据.AsyncTask类用于此目的.
public class ForecastFragment extends Fragment {
String imageUrl;
ListView listView;
List<WeatherForecastData> WeatherForecastDataList;
String IMG_URL = "http://api.openweathermap.org/img/w/";
Fragment fragment;
public ForecastFragment() {
// Required empty public constructor
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
//Inflate xml view and convert it to a View object
View rootView = inflater.inflate(R.layout.fragment_forecast, container, false);
//Initialise ListView.
listView = (ListView) rootView.findViewById(R.id.listView);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String temp = WeatherForecastDataList.get(position).getWeatherTemperature(); …Run Code Online (Sandbox Code Playgroud)