我在我的android项目中使用RecyclerView,其性能非常糟糕.从这里的答案,我尝试添加adapter.setHasStableIds(true);到我的代码.运行时,我收到一个错误:
java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.
Run Code Online (Sandbox Code Playgroud)
我的完整logCat如下;
09-22 22:22:23.634 1808-1808/com.revosleap.movielist E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.revosleap.movielist, PID: 1808
java.lang.IllegalStateException: Cannot change whether this adapter has stable IDs while the adapter has registered observers.
at android.support.v7.widget.RecyclerView$Adapter.setHasStableIds(RecyclerView.java:6749)
at com.revosleap.movielist.Utils.UrlUtils.GenreFetcher.getGenre(GenreFetcher.java:48)
at com.revosleap.movielist.MainActivity$2.onItemSelected(MainActivity.java:221)
at android.widget.AdapterView.fireOnSelected(AdapterView.java:1124)
at android.widget.AdapterView.access$200(AdapterView.java:54)
at android.widget.AdapterView$SelectionNotifier.run(AdapterView.java:1089)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:145)
at android.app.ActivityThread.main(ActivityThread.java:5942)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1400)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1195)
Run Code Online (Sandbox Code Playgroud)
我该如何解决这个问题?
我是使用房间持久性的新手,每当我尝试运行我的代码时都会出现此错误。编辑时没有显示错误,但我在 gradle 上遇到构建错误。我不得不复制粘贴代码,但似乎没有任何效果,这让我很失望。错误如下
error: An entity must have at least 1 field annotated with @PrimaryKey
Run Code Online (Sandbox Code Playgroud)
我该如何解决?我的代码如下;
package com.revosleap.dummy.DatabaseMov;
import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.arch.persistence.room.Ignore;
import android.arch.persistence.room.PrimaryKey;
@Entity
public class TodoListItem {
@Ignore
@PrimaryKey(autoGenerate = true)
private int id;
@ColumnInfo(name = "time")
private String time;
@ColumnInfo(name="title")
private String title;
public TodoListItem(){
}
public TodoListItem(String time, String title) {
this.time = time;
this.title = title;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
} …Run Code Online (Sandbox Code Playgroud)