Android Room + AsyncTask выдает ошибку doInBackground () при исполнении - PullRequest
0 голосов
/ 07 декабря 2018

Я пытаюсь интегрировать комнату в моем проекте с использованием asyntask для обработки фонового процесса.я не очень осведомлен об этом асинктасте с комнатой данных.когда я пытаюсь вставить фиктивные данные, мое приложение вылетает.здесь плохо приведу трассировку ошибки и код.Пожалуйста, помогите мне, где я делаю неправильно?Я попробовал это с отдельным проектом, но он тоже падает.помогите мне найти, как решить комнату с асинхронной.образец или решение более чем приветствуются.

public class ReceiptsRepo {

private ReceiptsDao receiptDao;
private LiveData<List<Receipts>> mAllReceipts;

public ReceiptsRepo(Application application) {
    AppDatabase appDatabase = AppDatabase.getINSTANCE(application);
    receiptDao = appDatabase.receiptsDao();
    mAllReceipts = receiptDao.retrunAllReceipts();
}

public void insertReceipt(Receipts receipts){
    new InsertReceiptAsyncTask(receiptDao).execute(receipts);
}

public void updateReceipts(Receipts receipts){
    new updateReceiptsAsyncTask(receiptDao).execute(receipts);
}

public void deleteReceipts(Receipts receipts){
    new deleteReceiptsAsyncTask(receiptDao).execute(receipts);
}

public void deleteAllReceipts(){
    new deleteAllReceiptsAsyncTask(receiptDao).execute();
}

public LiveData<List<Receipts>> getmAllReceipts() {
    return mAllReceipts;
}

private static class InsertReceiptAsyncTask extends AsyncTask<Receipts, Void, Void> {

    private ReceiptsDao mReceiptsDao;
    private Receipts receipts;

    private InsertReceiptAsyncTask(ReceiptsDao receiptsDao){
        mReceiptsDao = receiptsDao;
    }

    @Override
    protected Void doInBackground(final Receipts... receipts) {
        mReceiptsDao.addReceiptsItem(receipts[0]);
        return null;
    }
}

private static class updateReceiptsAsyncTask extends AsyncTask<Receipts,Void, Void>{

    private ReceiptsDao mReceiptsDao;

    private updateReceiptsAsyncTask(ReceiptsDao receiptsDao) {
        mReceiptsDao = receiptsDao;
    }

    @Override
    protected Void doInBackground(Receipts... receipts) {
        mReceiptsDao.editReceiptsItem(receipts[0]);
        return null;
    }
}

private static class deleteReceiptsAsyncTask extends AsyncTask<Receipts, Void, Void>{

    private ReceiptsDao mReceiptsDao;

    private deleteReceiptsAsyncTask(ReceiptsDao receiptsDao) {
        mReceiptsDao = receiptsDao;
    }

    @Override
    protected Void doInBackground(Receipts... receipts) {
        mReceiptsDao.deleteReceiptsItem(receipts[0]);
        return null;
    }
}

private static class deleteAllReceiptsAsyncTask extends AsyncTask<Void, Void, Void>{

    private ReceiptsDao mReceiptsDao;

    private deleteAllReceiptsAsyncTask(ReceiptsDao receiptsDao) {
        mReceiptsDao = receiptsDao;
    }

    @Override
    protected Void doInBackground(Void... voids) {
        mReceiptsDao.deleteAllReceipts();
        return null;
    }
}
}



@Entity(tableName = "receipts")
public class Receipts {

@PrimaryKey(autoGenerate = true)
private int contract_id;

private String contractCode;
private String mobileNumber;
private String amount;
private String vehicleNumber;
private Boolean isPending;
private String defaultInterest;
private String fullBalance;
private String arrearsAmount;
private String dueAmount;
private String lastPayment;
private String recoveryCharges;
private String settlementAmount;
private String arrearsAge;
private String dueDay;
private String lastpayDay;
private String debit;
private String tax;
private String type;

public Receipts(int contract_id, String contractCode, String mobileNumber, String vehicleNumber, boolean isPending, String defaultInterest, String amount, String fullBalance, String arrearsAmount, String dueAmount, String lastPayment, String recoveryCharges, String settlementAmount, String arrearsAge, String dueDay, String lastpayDay, String debit, String tax, String type) {
    this.contract_id = contract_id;
    this.contractCode = contractCode;
    this.mobileNumber = mobileNumber;
    this.amount = amount;
    this.vehicleNumber = vehicleNumber;
    this.isPending = isPending;
    this.defaultInterest = defaultInterest;
    this.fullBalance = fullBalance;
    this.arrearsAmount = arrearsAmount;
    this.dueAmount = dueAmount;
    this.lastPayment = lastPayment;
    this.recoveryCharges = recoveryCharges;
    this.settlementAmount = settlementAmount;
    this.arrearsAge = arrearsAge;
    this.dueDay = dueDay;
    this.lastpayDay = lastpayDay;
    this.debit = debit;
    this.tax = tax;
    this.type = type;
}

public int getContract_id() {
    return contract_id;
}

public void setContract_id(int contract_id) {
    this.contract_id = contract_id;
}

public String getContractCode() {
    return contractCode;
}

public String getMobileNumber() {
    return mobileNumber;
}

public void setMobileNumber(String mobileNumber) {
    this.mobileNumber = mobileNumber;
}

public String getAmount() {
    return amount;
}

public void setAmount(String amount) {
    this.amount = amount;
}

public String getVehicleNumber() {
    return vehicleNumber;
}

public void setVehicleNumber(String vehicleNumber) {
    this.vehicleNumber = vehicleNumber;
}

public Boolean getIsPending() {
    return isPending;
}

public void setIsPending(Boolean isPending) {
    this.isPending = isPending;
}

public void setContractCode(String contractCode) {
    this.contractCode = contractCode;
}

public String getDefaultInterest() {
    return defaultInterest;
}

public void setDefaultInterest(String default_interest) {
    this.defaultInterest = default_interest;
}

public String getFullBalance() {
    return fullBalance;
}

public void setFullBalance(String fullBalance) {
    this.fullBalance = fullBalance;
}

public String getArrearsAmount() {
    return arrearsAmount;
}

public void setArrearsAmount(String arrearsAmount) {
    this.arrearsAmount = arrearsAmount;
}

public String getDueAmount() {
    return dueAmount;
}

public void setDueAmount(String dueAmount) {
    this.dueAmount = dueAmount;
}

public String getLastPayment() {
    return lastPayment;
}

public void setLastPayment(String lastPayment) {
    this.lastPayment = lastPayment;
}

public String getRecoveryCharges() {
    return recoveryCharges;
}

public void setRecoveryCharges(String recoveryCharges) {
    this.recoveryCharges = recoveryCharges;
}

public String getSettlementAmount() {
    return settlementAmount;
}

public void setSettlementAmount(String settlementAmount) {
    this.settlementAmount = settlementAmount;
}

public String getArrearsAge() {
    return arrearsAge;
}

public void setArrearsAge(String arrearsAge) {
    this.arrearsAge = arrearsAge;
}

public String getDueDay() {
    return dueDay;
}

public void setDueDay(String dueDay) {
    this.dueDay = dueDay;
}

public String getLastpayDay() {
    return lastpayDay;
}

public void setLastpayDay(String lastpayDay) {
    this.lastpayDay = lastpayDay;
}

public String getDebit() {
    return debit;
}

public void setDebit(String debit) {
    this.debit = debit;
}

public String getTax() {
    return tax;
}

public void setTax(String tax) {
    this.tax = tax;
}

public String getType() {
    return type;
}

public void setType(String type) {
    this.type = type;
}

@Override
public boolean equals(Object o) {
    if (this == o) return true;
    if (!(o instanceof Receipts)) return false;
    Receipts receipts = (Receipts) o;
    if (contract_id != receipts.contract_id) return false;
    return contractCode != null ? contractCode.equals(receipts.contractCode) : receipts.contractCode == null;
}

@Override
public int hashCode() {

    int result = contract_id;
    result = 31 * result + (contractCode != null ? contractCode.hashCode() : 0);
    return result;
}

@NonNull
@Override
public String toString() {
    return "Receipts{" +
            "contract_id=" + contract_id +
            ", contractCode=" + contractCode +
            ", mobileNumber='" + mobileNumber + '\'' +
            ", amount='" + amount + '\'' +
            ", vehicleNumber='" + vehicleNumber + '\'' +
            ", isPending=" + isPending +
            ", defaultInterest='" + defaultInterest + '\'' +
            ", fullBalance='" + fullBalance + '\'' +
            ", arrearsAmount='" + arrearsAmount + '\'' +
            ", dueAmount='" + dueAmount + '\'' +
            ", lastPayment='" + lastPayment + '\'' +
            ", recoveryCharges='" + recoveryCharges + '\'' +
            ", settlementAmount='" + settlementAmount + '\'' +
            ", arrearsAge='" + arrearsAge + '\'' +
            ", dueDay='" + dueDay + '\'' +
            ", lastpayDay='" + lastpayDay + '\'' +
            ", debit='" + debit + '\'' +
            ", tax='" + tax + '\'' +
            ", type='" + type + '\'' +
            '}';
}
}

класс базы данных dao[![stacktrace

1 Ответ

0 голосов
/ 09 декабря 2018

Пожалуйста, попробуйте это и проверьте ваши журналы, что будет

public void insertReceipt(Receipts receipts){
    if(receipts!=null){
     new InsertReceiptAsyncTask(receiptDao).execute(receipts);
    }else{
      Log.d("Jamun"," Null");
    }
}

private static class InsertReceiptAsyncTask extends AsyncTask<Receipts, Void, Void> {

private ReceiptsDao mReceiptsDao;

private InsertReceiptAsyncTask(ReceiptsDao receiptsDao){
    mReceiptsDao = receiptsDao;
}

@Override
protected Void doInBackground(Receipts... receipts) {
    mReceiptsDao.addReceiptsItem(receipts[0]);
    return null;
  }
}
...