我正在尝试使用以下按钮单击将数据插入数据库:
在我的 MainActivity 中,我正在使用:
package com.example.trial;
public CityViewModel mcityViewModel;
@Override
protected void onCreate(Bundle savedInstanceState) {
ImageButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cityName = cityNameWeakReference.getText().toString();
City cityItem = new City(cityName, 5.0,4.0);
Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mcityViewModel.insert(cityItem);
}
});
为了便于查看与数据库相关的代码,我已经在com.example.trial.db
中定义了所有与数据库相关的文件(除了DbActivity
中显示了recyclerview
的文件)。
我的实体
package com.example.trial.db
Entity(tableName = "city_table")
public class City {
@NonNull
@PrimaryKey
@ColumnInfo(name = "city")
private String city = "Atlantis";
@NonNull
@ColumnInfo(name = "latitude")
private Double latitude = 0.0;
@NonNull
@ColumnInfo(name = "Longitude")
private Double longitude = 0.0;
public City(@NonNull String city, @NonNull Double latitude, @NonNull Double longitude) {
this.city = city;
this.latitude = latitude;
this.longitude = longitude;
}
... getter and setter for city, latitude and longitude ...
... not showing for brevity. If you think that is necessary, please ask...
道
package com.example.trial.db
@Dao
public interface CityDao{
@Insert(onConflict = OnConflictStrategy.IGNORE)
void insert(City city);
@Query("DELETE FROM city_table")
void deleteAll();
@Delete
void deleteCity(City city);
@Query("SELECT * from city_table LIMIT 1")
City[] getAnyCity();
@Query("SELECT * from city_table ORDER BY city ASC")
LiveData<List<City>> getAllCity();
}
和 ViewModel 是
package com.example.trial.db
public class CityViewModel extends AndroidViewModel {
private CityRepository mRepository;
private LiveData<List<City>> mAllCity;
public CityViewModel(Application application) {
super(application);
mRepository = new CityRepository(application);
mAllCity = mRepository.getAllCity();
}
public LiveData<List<City>> getAllCity() {
return mAllCity;
}
public void insert(City city) { mRepository.insert(city); }
public void deleteCity(City city) { mRepository.deleteCity(city); }
public void deleteAll() {
mRepository.deleteAll();
}
}
现在,当我运行代码时,我得到了一个错误
java.lang.NullPointerException: Attempt to invoke virtual method 'void com.example.trial.db.CityViewModel.insert(com.example.trial.db.City)' on a null object reference
snackbar
给出了一些值,这表明cityItem
也不为空,但是仍然出现此错误。
我对Java很陌生,并尝试从this codelabs
构建它。但是我无法在这里找出我在做什么错。
请帮助。
答案 0 :(得分:1)
您似乎没有在mcityViewModel
中初始化onCreate(Bundle savedInstanceState)
请在onCreate
方法中添加以下行:
mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);
@Override
protected void onCreate(Bundle savedInstanceState) {
// -------> Add this line here <-------
mcityViewModel = new ViewModelProvider(this).get(CityViewModel.class);
ImageButton fab = findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String cityName = cityNameWeakReference.getText().toString();
City cityItem = new City(cityName, 5.0,4.0);
Snackbar.make(view, cityName+cityItem, Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
mcityViewModel.insert(cityItem);
}
});
}