错误:不兼容的类型:如果使用android室((__cursor.isNull(null)),则无法转换为int

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了错误:不兼容的类型:如果使用android室((__cursor.isNull(null)),则无法转换为int相关的知识,希望对你有一定的参考价值。

我正在为我的android数据库使用空间。在这里,我在Customer和ServiceProvider类之间有很多对很多的关系,我曾用this tutorial来做到这一点,但在编译后却收到此错误:

/ home / omid / IntelliJIDEAProjects / Accounting / app / build / generated / ap_genic_sources / debug / out / com / omidmsl / accounting / db / CustomerDAO_Impl.java:69:错误:不兼容的类型:无法转换为int如果(!_cursor.isNull(null)){

并且提到此代码CustomerDAO_Impl.java(由编译器创建):

package com.omidmsl.accounting.db;

import android.database.Cursor;
import androidx.collection.ArrayMap;
import androidx.room.RoomDatabase;
import androidx.room.RoomSQLiteQuery;
import androidx.room.util.CursorUtil;
import androidx.room.util.DBUtil;
import androidx.room.util.StringUtil;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;
import java.lang.Override;
import java.lang.String;
import java.lang.StringBuilder;
import java.lang.SuppressWarnings;
import java.util.ArrayList;
import java.util.List;
import java.util.Set;

@SuppressWarnings({"unchecked", "deprecation"})
public final class CustomerDAO_Impl implements CustomerDAO {
  private final RoomDatabase __db;

  public CustomerDAO_Impl(RoomDatabase __db) {
    this.__db = __db;
  }

  @Override
  public List<Customer> getCustomers() {
    final String _sql = "SELECT * FROM Customer";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
      final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final Customer _item;
        _item = new Customer();
        final String _tmpCustomerName;
        _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
        _item.setCustomerName(_tmpCustomerName);
        final String _tmpPhoneNumber;
        _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
        _item.setPhoneNumber(_tmpPhoneNumber);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  @Override
  public List<CustomerOfServiceProvider> getCustomersOfServiceProvider() {
    final String _sql = "SELECT * FROM Customer";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 0);
    __db.assertNotSuspendingTransaction();
    __db.beginTransaction();
    try {
      final Cursor _cursor = DBUtil.query(__db, _statement, true, null);
      try {
        final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
        final ArrayMap<String, ArrayList<Customer>> _collectionCustomers = new ArrayMap<String, ArrayList<Customer>>();
        while (_cursor.moveToNext()) {
          if (!_cursor.isNull(null)) {
            final String _tmpKey = _cursor.getString(null);
            ArrayList<Customer> _tmpCustomersCollection = _collectionCustomers.get(_tmpKey);
            if (_tmpCustomersCollection == null) {
              _tmpCustomersCollection = new ArrayList<Customer>();
              _collectionCustomers.put(_tmpKey, _tmpCustomersCollection);
            }
          }
        }
        _cursor.moveToPosition(-1);
        __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_collectionCustomers);
        final List<CustomerOfServiceProvider> _result = new ArrayList<CustomerOfServiceProvider>(_cursor.getCount());
        while(_cursor.moveToNext()) {
          final CustomerOfServiceProvider _item;
          final Business _tmpServiceProvider;
          if (! (_cursor.isNull(_cursorIndexOfPhoneNumber))) {
            _tmpServiceProvider = new Business();
            final String _tmpPhoneNumber;
            _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
            _tmpServiceProvider.setPhoneNumber(_tmpPhoneNumber);
          }  else  {
            _tmpServiceProvider = null;
          }
          ArrayList<Customer> _tmpCustomersCollection_1 = null;
          if (!_cursor.isNull(null)) {
            final String _tmpKey_1 = _cursor.getString(null);
            _tmpCustomersCollection_1 = _collectionCustomers.get(_tmpKey_1);
          }
          if (_tmpCustomersCollection_1 == null) {
            _tmpCustomersCollection_1 = new ArrayList<Customer>();
          }
          _item = new CustomerOfServiceProvider();
          _item.setServiceProvider(_tmpServiceProvider);
          _item.setCustomers(_tmpCustomersCollection_1);
          _result.add(_item);
        }
        __db.setTransactionSuccessful();
        return _result;
      } finally {
        _cursor.close();
        _statement.release();
      }
    } finally {
      __db.endTransaction();
    }
  }

  @Override
  public List<Customer> getCustomer(final String cName) {
    final String _sql = "SELECT * FROM Customer WHERE customerName = ?";
    final RoomSQLiteQuery _statement = RoomSQLiteQuery.acquire(_sql, 1);
    int _argIndex = 1;
    if (cName == null) {
      _statement.bindNull(_argIndex);
    } else {
      _statement.bindString(_argIndex, cName);
    }
    __db.assertNotSuspendingTransaction();
    final Cursor _cursor = DBUtil.query(__db, _statement, false, null);
    try {
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndexOrThrow(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndexOrThrow(_cursor, "phoneNumber");
      final List<Customer> _result = new ArrayList<Customer>(_cursor.getCount());
      while(_cursor.moveToNext()) {
        final Customer _item;
        _item = new Customer();
        final String _tmpCustomerName;
        _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
        _item.setCustomerName(_tmpCustomerName);
        final String _tmpPhoneNumber;
        _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
        _item.setPhoneNumber(_tmpPhoneNumber);
        _result.add(_item);
      }
      return _result;
    } finally {
      _cursor.close();
      _statement.release();
    }
  }

  private void __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(final ArrayMap<String, ArrayList<Customer>> _map) {
    final Set<String> __mapKeySet = _map.keySet();
    if (__mapKeySet.isEmpty()) {
      return;
    }
    // check if the size is too big, if so divide;
    if(_map.size() > RoomDatabase.MAX_BIND_PARAMETER_CNT) {
      ArrayMap<String, ArrayList<Customer>> _tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(androidx.room.RoomDatabase.MAX_BIND_PARAMETER_CNT);
      int _tmpIndex = 0;
      int _mapIndex = 0;
      final int _limit = _map.size();
      while(_mapIndex < _limit) {
        _tmpInnerMap.put(_map.keyAt(_mapIndex), _map.valueAt(_mapIndex));
        _mapIndex++;
        _tmpIndex++;
        if(_tmpIndex == RoomDatabase.MAX_BIND_PARAMETER_CNT) {
          __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
          _tmpInnerMap = new ArrayMap<String, ArrayList<Customer>>(RoomDatabase.MAX_BIND_PARAMETER_CNT);
          _tmpIndex = 0;
        }
      }
      if(_tmpIndex > 0) {
        __fetchRelationshipCustomerAscomOmidmslAccountingModelsCustomer(_tmpInnerMap);
      }
      return;
    }
    StringBuilder _stringBuilder = StringUtil.newStringBuilder();
    _stringBuilder.append("SELECT `Customer`.`customerName` AS `customerName`,`Customer`.`phoneNumber` AS `phoneNumber`,_junction.`businessName` FROM `Service` AS _junction INNER JOIN `Customer` ON (_junction.`customerName` = `Customer`.`customerName`) WHERE _junction.`businessName` IN (");
    final int _inputSize = __mapKeySet.size();
    StringUtil.appendPlaceholders(_stringBuilder, _inputSize);
    _stringBuilder.append(")");
    final String _sql = _stringBuilder.toString();
    final int _argCount = 0 + _inputSize;
    final RoomSQLiteQuery _stmt = RoomSQLiteQuery.acquire(_sql, _argCount);
    int _argIndex = 1;
    for (String _item : __mapKeySet) {
      if (_item == null) {
        _stmt.bindNull(_argIndex);
      } else {
        _stmt.bindString(_argIndex, _item);
      }
      _argIndex ++;
    }
    final Cursor _cursor = DBUtil.query(__db, _stmt, false, null);
    try {
      final int _itemKeyIndex = 2; // _junction.businessName;
      if (_itemKeyIndex == -1) {
        return;
      }
      final int _cursorIndexOfCustomerName = CursorUtil.getColumnIndex(_cursor, "customerName");
      final int _cursorIndexOfPhoneNumber = CursorUtil.getColumnIndex(_cursor, "phoneNumber");
      while(_cursor.moveToNext()) {
        if (!_cursor.isNull(_itemKeyIndex)) {
          final String _tmpKey = _cursor.getString(_itemKeyIndex);
          ArrayList<Customer> _tmpRelation = _map.get(_tmpKey);
          if (_tmpRelation != null) {
            final Customer _item_1;
            _item_1 = new Customer();
            if (_cursorIndexOfCustomerName != -1) {
              final String _tmpCustomerName;
              _tmpCustomerName = _cursor.getString(_cursorIndexOfCustomerName);
              _item_1.setCustomerName(_tmpCustomerName);
            }
            if (_cursorIndexOfPhoneNumber != -1) {
              final String _tmpPhoneNumber;
              _tmpPhoneNumber = _cursor.getString(_cursorIndexOfPhoneNumber);
              _item_1.setPhoneNumber(_tmpPhoneNumber);
            }
            _tmpRelation.add(_item_1);
          }
        }
      }
    } finally {
      _cursor.close();
    }
  }
}

这是我的其余代码:Customer.java:

package com.omidmsl.accounting.models;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.PrimaryKey;

@Entity(tableName = "Customer")
public class Customer {

    public static final String KEY_NAME = "customer_name";
    public static final String KEY_PHONE_NUMBER = "phone_number";

    @NonNull
    @PrimaryKey
    private String customerName;
    private String phoneNumber;

    public String getCustomerName() {
        return customerName;
    }

    public void setCustomerName(String customerName) {
        this.customerName = customerName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }
}

CustomerDAO.java:

package com.omidmsl.accounting.db;

import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.Customer;
import com.omidmsl.accounting.models.serviceprovider.CustomerOfServiceProvider;

import java.util.List;

@Dao
public interface CustomerDAO {

    @Query("SELECT * FROM Customer")
    public List<Customer> getCustomers();

    @Transaction
    @Query("SELECT * FROM Customer")
    public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();

    @Query("SELECT * FROM Customer WHERE customerName = :cName")
    public List<Customer> getCustomer(String cName);
}

Business.java:

package com.omidmsl.accounting.models;

import androidx.annotation.NonNull;
import androidx.room.Entity;
import androidx.room.*;


@Entity(tableName = "Business")
public class Business {

    public static final String KEY_TYPE = "type";
    public static final String KEY_NAME = "business_name";
    public static final String KEY_PHONE_NUMBER = "phone_number";

    private int type;
    @PrimaryKey
    @NonNull
    private String businessName;
    private String phoneNumber;
    @Ignore
    private long costs;
    @Ignore
    private long buys;

    public Business(int type) {
        this.type = type;
    }

    public int getType() {
        return type;
    }

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

    public Business() {
        businessName = "";
    }

    public String getBusinessName() {
        return businessName;
    }

    public void setBusinessName(String businessName) {
        this.businessName = businessName;
    }

    public String getPhoneNumber() {
        return phoneNumber;
    }

    public void setPhoneNumber(String phoneNumber) {
        this.phoneNumber = phoneNumber;
    }

    public long getCosts() {
        return costs;
    }

    public void setCosts(long costs) {
        this.costs = costs;
    }

    public long getBuys() {
        return buys;
    }

    public void setBuys(long buys) {
        this.buys = buys;
    }
}

ServiceProviderDAO.java:

package com.omidmsl.accounting.db;

import androidx.room.Dao;
import androidx.room.Query;
import androidx.room.Transaction;
import com.omidmsl.accounting.models.serviceprovider.ServiceProviderOfCustomer;

import java.util.List;

@Dao
public interface ServiceProviderDAO {
    @Transaction
    @Query("SELECT * FROM Business")
    public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();
}

CustomersOfServiceProvider.java:

package com.omidmsl.accounting.models.serviceprovider;

import androidx.room.*;
import com.omidmsl.accounting.models.Business;
import com.omidmsl.accounting.models.Customer;

import java.util.List;

public class CustomerOfServiceProvider {
    @Embedded
    private Business serviceProvider;
    @Relation(
            parentColumn = "businessName",
            entityColumn = "customerName",
            associateBy = @Junction(Service.class)
    )
    private List<Customer> customers;

    public Business getServiceProvider() {
        return serviceProvider;
    }

    public void setServiceProvider(Business serviceProvider) {
        this.serviceProvider = serviceProvider;
    }

    public List<Customer> getCustomers() {
        return customers;
    }

    public void setCustomers(List<Customer> customers) {
        this.customers = customers;
    }
}

请帮助我

答案

谢谢@ianhanniballake。问题解决了!。在CustomerDAO文件中,我发现此问题:

@Transaction
    @Query("SELECT * FROM Customer")
    public List<CustomerOfServiceProvider> getCustomersOfServiceProvider();

此列表的类型必须是ServiceProvidersOfCustomer:

@Transaction
    @Query("SELECT * FROM Customer")
    public List<ServiceProviderOfCustomer> getServiceProvidersOfCustomer();

以上是关于错误:不兼容的类型:如果使用android室((__cursor.isNull(null)),则无法转换为int的主要内容,如果未能解决你的问题,请参考以下文章

为啥此错误显示“不兼容的类型”。 “必需:android.support.v4.app.Fragment”?

win10系统H3C云实验室模拟器安装故障解决

FusedLocationApi.requestLocationUpdates 给出不兼容的类型错误

Android Studio不兼容的类型

mypy 错误:返回值类型不兼容(得到“object”,预期“Dict [Any, Any]”)

Android:片段无法转换为上下文[重复]