所以我有一个要查询的数据库,但是当我的活动调用查询方法时,我一直得到空对象
这里锻炼班
@Entity(tableName = "workout_table")
public class Workout {
@PrimaryKey
@NonNull
private String name;
@ColumnInfo(name = "number_of_sets")
private int numOfSets;
@ColumnInfo(name = "workout_duration")
private int workDuration;
@ColumnInfo(name = "rest_duration")
private int restDuration;
@ColumnInfo(name = "selected_workout")
private boolean seletectedWorkout;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getNumOfSets() {
return numOfSets;
}
public void setNumOfSets(int numOfSets) {
this.numOfSets = numOfSets;
}
public int getWorkDuration() {
return workDuration;
}
public void setWorkDuration(int workDuration) {
this.workDuration = workDuration;
}
public int getRestDuration() {
return restDuration;
}
public void setRestDuration(int restDuration) {
this.restDuration = restDuration;
}
public boolean isSeletectedWorkout() {
return seletectedWorkout;
}
public void setSeletectedWorkout(boolean seletectedWorkout) {
this.seletectedWorkout = seletectedWorkout;
}
public Workout(String name, int numOfSets, int workDuration, int restDuration) {
this.name = name;
this.numOfSets = numOfSets;
this.workDuration = workDuration;
this.restDuration = restDuration;
}
public Workout(String name, int numOfSets, int workDuration, int restDuration, boolean
selectedWorkout) {
this.name = name;
this.numOfSets = numOfSets;
this.workDuration = workDuration;
this.restDuration = restDuration;
this.seletectedWorkout = selectedWorkout;
}
}
我是DAO
@Dao
public interface WorkoutDao {
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(Workout workout);
@Delete
void delete(Workout workout);
@Query("select * from workout_table order by name ASC")
LiveData<List<Workout>> getAlphabetizedWorkouts();
@Query("select * from workout_table where selected_workout")
LiveData<Workout> getSelectedWorkout();
}
这是存储库
public class WorkoutRepository {
private WorkoutDao mWorkoutDao;
private LiveData<List<Workout>> mAllWorkouts;
private LiveData<Workout> mSelectedWorkout;
private static volatile WorkoutRepository sInstance = null;
WorkoutRepository(Application application) {
WorkoutRoomDatabase db = WorkoutRoomDatabase.getDatabase(application);
mWorkoutDao = db.workoutDao();
mAllWorkouts = mWorkoutDao.getAlphabetizedWorkouts();
}
LiveData<List<Workout>> getAllWorkouts() {
return mAllWorkouts;
}
LiveData<Workout> getSelectedWorkout() {
return mSelectedWorkout;
}
void insert(Workout workout) {
WorkoutRoomDatabase.databaseWriteExecutor.execute(() -> {
mWorkoutDao.insert(workout);
});
}
}
这是MainActivity类字段
private MainActivityViewModel mMainActivityViewModel;
private LiveData<Workout> defaultWorkout;
这里是MainActivity onCreate,方法调用在其中
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mCountDown = findViewById(R.id.main_countdown);
mButtonStartPause = findViewById(R.id.button_start_pause);
mButtonReset = findViewById(R.id.button_reset);
mNewWorkout = findViewById(R.id.new_workout);
mWorkouts = findViewById(R.id.my_workouts);
mWorkRestLabel = findViewById(R.id.work_rest_label);
mMainActivityViewModel = new ViewModelProvider(this).get(MainActivityViewModel.class);
defaultWorkout = mMainActivityViewModel.getSelectedWorkout();
//set up the workout parameters
mNumberOfSets = defaultWorkout.getValue().getNumOfSets();
mWorkoutDuration = (int) (defaultWorkout.getValue().getWorkDuration())* 1000;
mRestDuration = (int) (defaultWorkout.getValue().getRestDuration()) * 1000;
}
每次我从MainActivity调用getSelectedworkout时,都会得到一个空指针异常方法调用或查询有问题吗?
答案 0 :(得分:1)
在您的Dao
中,应按以下方式更改最后一个方法getSelectedWorkout()
:
@Query("select * from workout_table where selected_workout= :workout")
LiveData<Workout> getSelectedWorkout(int workout);