java.lang.Boolean 无法转换为 ba.store.models.Merchants

标签 java android android-studio android-fragments

我有一个listview,其中adapter扩展了Fragment中的BaseAdapter。在该 listview 上,我实现了 scrollview 监听器,用于检测用户何时到达 listview 的末尾。在这种情况下,将加载更多数据。另外,我在该 fragment 上有一个按钮,单击该按钮将重置所有过滤器并重新加载包含起始项目的列表。问题是当用户滚动底部并同时(在加载新元素之前)按下按钮应用程序将崩溃并出现以下错误:

java.lang.Boolean cannot be cast to ba.store.models.Merchants

这是分解的代码:

 @Override
public View getView(int position, View convertView, ViewGroup parent) {
    View view = null;
    Integer identity;
    ViewHolder viewHolder = null;
    notifyDataSetChanged();
    if (convertView == null) {
        view = LayoutInflater.from(parent.getContext()).inflate(R.layout.sales_place_item, parent, false);
        viewHolder = new ViewHolder(view);
        view.setTag(viewHolder);

    } else {
        view = convertView;
        viewHolder = (ViewHolder) view.getTag();

    }
    Merchants merchants = (Merchants) getItem(position);
    if (merchants != null) {
        identity = merchants.getMerchantId();
        viewHolder.merchant_name.setText(merchants.getMerchantName());
        viewHolder.merchant_category.setText(merchants.getCategoryName().toUpperCase());
        viewHolder.border_limit.setText(merchants.getBorderLimitAmount().toString() + " KM");
        viewHolder.end_loyalty.setText(merchants.getEndLoyaltyPoints().toString());
        viewHolder.begin_loyalty.setText(merchants.getStartLoyaltyPoints().toString());

        if (merchants.getImagePath().equals(""))
            Picasso.with(view.getContext()).load(R.drawable.placeholder_sales).fit().into(viewHolder.merchant_image);
        else
            Picasso.with(view.getContext()).load(merchants.getImagePath()).fit().into(viewHolder.merchant_image);

    }


    return view;

}

获取项目方法:

@Override
public Object getItem(int position) {
    if (merchants.size() > 0) {
        return merchants.get(position);
    } else
        return false;
}

最佳答案

问题在于您的 getItem() 方法。

如果没有数据可显示,该方法将返回 false,一个 boolean 值。这会破坏 getview() 中的强制转换。

getItem() 方法更改为:

@Override
public Object getItem(int position) {
    if (merchants.size() > 0)
        return merchants.get(position);
    else
        return null;
}

并在getView()中,替换:

 Merchants merchants = (Merchants) getItem(position);
    if (merchants != null) {
        identity = merchants.getMerchantId();
        ...

与:

if (getItem(position) != null) {
    Merchants merchants = (Merchants) getItem(position);
    identity = merchants.getMerchantId();
    ...

这将修复崩溃问题,但您还必须检查为什么 merchants.getSize() 为 0。

关于java.lang.Boolean 无法转换为 ba.store.models.Merchants,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39765772/

相关文章:

java - Oozie Java Action System.out

android - 在 chrome 远程调试期间未定义 InspectorFrontEndApi

android - ProGuard 不能与 okhttp 一起使用

android - 错误的 TableLayout 重量

java - 不使用 XPath 从 webtable 内的元素获取文本(Selenium Web 驱动程序 + Java)

Java:读者和编码

java - 使用 webdriver java 为所有浏览器运行 selenium 测试,无需打开任何浏览器

android:layout_height 不适用于 CardView

android - 我的应用程序在我的设备上无法正常运行

java - 如何在android中制作一个按钮来打开一个新的 Activity ,并且这个 Activity 不仅仅是一个默认 Activity ,它是一个自定义 Activity