【问题标题】:App crashing when I try to populate a list view with a Sql table当我尝试使用 Sql 表填充列表视图时应用程序崩溃
【发布时间】:2014-11-24 03:26:19
【问题描述】:

我正在尝试通过SimpleCursorAdapter 使用 SQL 中的表中的数据填充列表视图,但每次活动打开时,它都会使应用程序崩溃。由于某种原因,Android Studio 不允许我查看错误日志。有人可以告诉我这段代码到底有什么问题吗?它在我的onCreate 方法中。

db = openOrCreateDatabase("namesAndscoresAndtimes", SQLiteDatabase.CREATE_IF_NECESSARY, null);
c = db.query("namesAndscoresAndtimes", null, null, null, null, null, null);
String[] fromCols = { "name", "score", "time", "percentile" };
int[] toViews = { R.id.name, R.id.score, R.id.time, R.id.points };
SimpleCursorAdapter sca = new SimpleCursorAdapter(this, R.layout.scr, c, fromCols, toViews, 0);
ListView lv = (ListView) findViewById(R.id.scoretables);
lv.setAdapter(sca);

【问题讨论】:

  • 你可以通过android studio中的“Android Device Monitor”看到Longcat。

标签: java android database listview simplecursoradapter


【解决方案1】:

可能你错了,

c = db.query("namesAndscoresAndtimes", null, null, null, null, null, null);

第一个参数是table name 而不是database name

【讨论】:

  • 我明白你的想法,但是数据库名和表名实际上是一样的,谢谢。
【解决方案2】:

没关系,我的表需要一个 _id 列才能在其上使用 SimpleCursorAdapter。所以我最终创建了一个新数据库(我也可以创建一个新表)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-08
    • 1970-01-01
    • 1970-01-01
    • 2016-07-19
    相关资源
    最近更新 更多