You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用CursorAdapter子类时App跳转ListView页面点击崩溃问题排查

点击ListView条目时App崩溃,求排查修复方法

点击ListView的条目跳转页面时,我的App发生崩溃,无法确定问题所在及修复方法。以下是相关代码:

CursorAdapter子类代码

package com.example.marhbabikapp;

import android.content.Context;
import android.database.Cursor;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CursorAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;

public class DestinationCursorAdapter extends CursorAdapter {
    MyDBHandler myDBHandler;

    public DestinationCursorAdapter(Context context, Cursor cursor,int flags ,MyDBHandler dbHandler) {
        super(context, cursor, flags);
        this.myDBHandler = dbHandler;;
    }

    @Override
    public View newView(Context context, Cursor cursor, ViewGroup parent) {
        return LayoutInflater.from(context).inflate(R.layout.item_destinationtype, parent, false);
    }

    @Override
    public void bindView(View view, Context context, Cursor cursor) {
        TextView nameTextView = view.findViewById(R.id.destinationTextViewName);
        TextView descriptionTextView = view.findViewById(R.id.destinationTextViewDescription);
        ImageView imageView = view.findViewById(R.id.destinationImageView);

        String name = cursor.getString(cursor.getColumnIndexOrThrow(MyDBHandler.DESTINATION_NAME));
        String description = cursor.getString(cursor.getColumnIndexOrThrow(MyDBHandler.DESTINATION_DESCRIPTION));
        String imageUrl = cursor.getString(cursor.getColumnIndexOrThrow(MyDBHandler.PHOTO_URL));

        nameTextView.setText(name);
        descriptionTextView.setText(description);

        Glide.with(context)
                .load(imageUrl)
                //.placeholder(R.drawable.placeholder_image) // Placeholder image while loading
                //.error(R.drawable.error_image) // Error image if Glide fails to load
                .into(imageView);
    }
}

Types_screen Activity代码

package com.example.marhbabikapp;

import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import java.util.Locale;

public class Types_screen extends AppCompatActivity {

    private ListView mylistView;
    private MyDBHandler dbHandler;
    private DestinationCursorAdapter destinationCursorAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_types_screen);

        Intent intent = getIntent();
        String type = intent.getStringExtra("DESTINATION_TYPE");

        mylistView = findViewById(R.id.listViewDestinations);
        dbHandler = new MyDBHandler(this);

        Cursor cursor = null;

        if ("Historical".equals(type.trim())) {
            cursor = dbHandler.getHistoricalDestinationsCursor();
        } else if ("Natural".equals(type.trim())) {
            cursor = dbHandler.getNaturalDestinationsCursor();
        } else if ("Coastal".equals(type.trim())) {
            cursor = dbHandler.getCoastalDestinationsCursor();
        } else if ("Modern".equals(type.trim())) {
            cursor = dbHandler.getModernDestinationsCursor();
        } else if ("Cultural".equals(type.trim())) {
            cursor = dbHandler.getCulturalDestinationsCursor();
        } else if ("Saharian".equals(type.trim())) {
            cursor = dbHandler.getSaharianDestinationsCursor();
        } else if ("Snowy".equals(type.trim())) {
            cursor = dbHandler.getSnowyDestinationsCursor();
        }

            destinationCursorAdapter = new DestinationCursorAdapter(this, cursor, 0, dbHandler);
            mylistView.setAdapter(destinationCursorAdapter);

}
}

数据库查询方法示例

public Cursor getSnowyDestinationsCursor() {
    SQLiteDatabase db = this.getReadableDatabase();

    String[] projection = {
            DESTINATION_NAME,
            DESTINATION_DESCRIPTION,
            PHOTO_URL,
            REGION
    };

    String selection = SNOWY + " = ?";
    String[] selectionArgs = { "1" };

    return db.query(
            TABLE_DESTINATIONS,
            projection,
            selection,
            selectionArgs,
            null,
            null,
            null
    );
}

我尝试重新创建一个CursorAdapter子类,但问题并未解决。

内容的提问来源于stack exchange,提问作者abdouz19

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.01 09:34:55