在房间数据库中插入值

Posted

技术标签:

【中文标题】在房间数据库中插入值【英文标题】:Insert value in Room database 【发布时间】:2020-06-13 07:55:29 【问题描述】:

我正在尝试使用按钮单击将数据插入数据库:

在我的 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 构建它。但我无法找出我在这里做错了什么。

请帮忙。

【问题讨论】:

【参考方案1】:

您似乎没有在 onCreate(Bundle savedInstanceState) 中初始化 mcityViewModel

请在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);
      
    );

【讨论】:

以上是关于在房间数据库中插入值的主要内容,如果未能解决你的问题,请参考以下文章

如何检查数据是不是插入到房间数据库中

如何将行从另一个数据库文件插入到房间数据库中?

触发错误:ORA-01403 插入时找不到数据

改造成功后将数据插入房间数据库,协程问题

删除房间内数据库

在 QTableView 中插入行