【问题标题】: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 构建它。但我无法找出我在这里做错了什么。

请帮忙。

【问题讨论】:

    标签: android android-room


    【解决方案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);
          }
        });
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-01-12
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多