错误:NOT NULL约束失败:centrals.categoryId(代码1299 SQLITE_CONSTRAINT_NOTNULL)

时间:2018-12-13 20:14:33

标签: java android

当我创建recents片段并添加新代码时,我遇到了这个问题: NOT NULL constraint failed recents.categoryId

我该如何解决?

MyRecyclearAdapter

public class MyRecyclearAdapter extends RecyclerView.Adapter<ListWallpaperViewHolder> {

private Context context;
private List<Recents> recents;

public MyRecyclearAdapter(Context context, List<Recents> recents) {
    this.context = context;
    this.recents = recents;
}

@NonNull
@Override
public ListWallpaperViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
    View itemView = LayoutInflater.from(viewGroup.getContext())
            .inflate(R.layout.layout_wallpaper_item,viewGroup,false);
    int height = viewGroup.getMeasuredHeight()/2;
    itemView.setMinimumHeight(height);
    return  new ListWallpaperViewHolder(itemView);
}

@Override
public void onBindViewHolder(@NonNull final ListWallpaperViewHolder holder, final int position) {

    Picasso.get()
            .load(recents.get(position).getImageLink())
            .networkPolicy(NetworkPolicy.OFFLINE)
            .into(holder.wallpaper, new Callback() {
                @Override
                public void onSuccess() {

                }

                @Override
                public void onError(Exception e) {
                    Picasso.get()
                            .load(recents.get(position).getImageLink())
                            .error(R.drawable.ic_terrain_black_24dp)

                            .into(holder.wallpaper, new Callback() {
                                @Override
                                public void onSuccess() {

                                }

                                @Override
                                public void onError(Exception e) {
                                    Log.e("ERROR_ULTRAHD","could not fetch image");
                                }
                            });

                }
            });
    holder.setItemClickListener(new ItemClickListener() {
        @Override
        public void onClick(View view, int position) {

            Intent intent = new Intent(context,ViewWalllpaper.class);
            WallpaperItem wallpaperItem = new WallpaperItem();
            wallpaperItem.setCategoryid(recents.get(position).getCategoryId());
            wallpaperItem.setImageLink(recents.get(position).getImageLink());
            Common.select_background = wallpaperItem;
            context.startActivity(intent);


        }
    });

}

@Override
public int getItemCount() {
    return recents.size();
}
   }

这是我的IRecentsDataSource

public interface IRecentsDataSource {
Flowable <List<Recents>> getAllRecents();
void insertRecents(Recents...recents);
void updateRecents(Recents...recents);
void deleteRecents(Recents...recents);
void deleteAllRecents();

}

** 这是我的 LatestRepository **

public class RecentRepository implements IRecentsDataSource {


private IRecentsDataSource mLocalDataSource;
private static RecentRepository instance;

public RecentRepository(IRecentsDataSource mLocalDataSource) {
    this.mLocalDataSource = mLocalDataSource;
}

public static RecentRepository getInstance (IRecentsDataSource mLocalDataSource){
    if (instance == null)
        instance = new RecentRepository(mLocalDataSource);
    return instance;
}


@Override
public Flowable<List<Recents>> getAllRecents() {
    return mLocalDataSource.getAllRecents();
}

@Override
public void insertRecents(Recents... recents) {
       mLocalDataSource.insertRecents(recents);
}

@Override
public void updateRecents(Recents... recents) {
       mLocalDataSource.updateRecents(recents);
}

@Override
public void deleteRecents(Recents... recents) {
    mLocalDataSource.deleteRecents(recents);
}

@Override
public void deleteAllRecents() {
     mLocalDataSource.deleteAllRecents();
}
}

LocalDatabase

@Database(entities = Recents.class,version = DATABASE_VERSION)
 public abstract class LocalDatabase extends RoomDatabase {

public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME="UltraHDWallpapers";

public abstract RecentsDAO recentsDAO();

private static LocalDatabase instance;

public static LocalDatabase getInstance(Context context)
{
    if (instance == null)
    {
        instance = 
 Room.databaseBuilder(context,LocalDatabase.class,DATABASE_NAME)
                .fallbackToDestructiveMigration()
                .build();
    }
    return instance;
}
}

RecentsDAO

@Dao
public interface RecentsDAO {
@Query("SELECT * FROM recents ORDER BY saveTime DESC LIMIT 10")
Flowable<List<Recents>> getAllRecents();

@Insert
void insertRecents(Recents...recents);

@Update
void updateRecents(Recents...recents);

@Delete
void deleteRecents(Recents...recents);

@Query("DELETE FROM recents")
void deleteAllRecents();
}

RecentsDataSource

public class RecentsDataSource implements IRecentsDataSource {

private RecentsDAO recentsDAO;
private static RecentsDataSource instance;

public RecentsDataSource(RecentsDAO recentsDAO) {
    this.recentsDAO = recentsDAO;
}

public static RecentsDataSource getInstance(RecentsDAO recentsDAO)
{
    if (instance == null)
        instance = new RecentsDataSource(recentsDAO);
    return instance;
}

@Override
public Flowable<List<Recents>> getAllRecents() {
    return recentsDAO.getAllRecents();
}

@Override
public void insertRecents(Recents... recents) {
       recentsDAO.insertRecents(recents);
}

@Override
public void updateRecents(Recents... recents) {
       recentsDAO.updateRecents(recents);
}

@Override
public void deleteRecents(Recents... recents) {
       recentsDAO.deleteRecents(recents);
}

@Override
public void deleteAllRecents() {
       recentsDAO.deleteAllRecents();
}
}

最近

import android.arch.persistence.room.ColumnInfo;
import android.arch.persistence.room.Entity;
import android.support.annotation.NonNull;

@Entity (tableName = "recents",primaryKeys = {"imageLink","categoryId"})
 public class Recents {

@ColumnInfo (name="imageLink")
@NonNull
private String imageLink;

@ColumnInfo (name="categoryId")
@NonNull
private String categoryId;

@ColumnInfo (name="saveTime")
private String saveTime;

public Recents(@NonNull String imageLink, @NonNull String categoryId, String  
saveTime) {
    this.imageLink = imageLink;
    this.categoryId = categoryId;
    this.saveTime = saveTime;
}

@NonNull
public String getImageLink() {
    return imageLink;
}

public void setImageLink(@NonNull String imageLink) {
    this.imageLink = imageLink;
}

@NonNull
public String getCategoryId() {
    return categoryId;
}

public void setCategoryId(@NonNull String categoryId) {
    this.categoryId = categoryId;
}

public String getSaveTime() {
    return saveTime;
}

public void setSaveTime(String saveTime) {
    this.saveTime = saveTime;
}
}

有人请帮我吗?

0 个答案:

没有答案