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

Android ListView无法更新显示标题问题排查求助

排查你的新闻阅读器ListView无内容问题

嘿,我仔细扒了你的代码,虽然Android Monitor没报错,但ListView空着的问题大概率出在几个容易踩的坑里,咱们一步步捋清楚:

1. 最可能的元凶:缺少网络权限

如果你的AndroidManifest.xml里没加网络权限,所有网络请求会静默失败——不会报错,但就是拿不到数据,数据库自然是空的,ListView也就没东西显示。

赶紧检查下,在<application>标签之前加上:

<uses-permission android:name="android.permission.INTERNET"/>
<!-- 可选,加这个能帮你检查网络状态,避免无网时白忙活 -->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>

2. 后台任务的异常没被正确捕获

你在DownloadTask里用了e.printStackTrace(),但AsyncTask的后台线程异常有时候不会显示在Android Monitor的默认日志里,导致你以为没报错,但其实请求或解析环节已经炸了。

把所有catch块里的e.printStackTrace()替换成带标签的日志,方便定位:

catch (MalformedURLException e) {
    Log.e("DownloadError", "Invalid URL", e);
} catch (IOException e) {
    Log.e("DownloadError", "Network IO failed", e);
} catch (JSONException e) {
    Log.e("DownloadError", "JSON parse error", e);
}

这样就能在Logcat里清晰看到哪个环节出问题了。

3. 数据库查询与数据填充的验证

先确认数据库里到底有没有数据,在updateListView里加几个日志,同时别忘了关闭Cursor避免内存泄漏:

public void updateListView(){
    Cursor c = articleDB.rawQuery("SELECT * FROM articles", null);
    int contentIndex = c.getColumnIndex("content");
    int titleIndex = c.getColumnIndex("title");
    
    // 打印查询到的条目数
    Log.d("DB_CHECK", "Total rows in articles: " + c.getCount());
    
    if(c.moveToFirst()){
        titles.clear();
        content.clear();
        do{
            titles.add(c.getString(titleIndex));
            content.add(c.getString(contentIndex));
        }while (c.moveToNext());
        Log.d("LIST_UPDATE", "Titles loaded: " + titles.size());
        arrayAdapter.notifyDataSetChanged();
    } else {
        // 没有数据时的提示
        Log.d("DB_CHECK", "No data found in articles table");
    }
    // 关闭Cursor,避免内存泄漏
    c.close();
}

4. 文章内容请求的容错优化

你直接请求文章的URL并把整个网页当内容插入,但有些网页可能重定向、编码不对,或者请求失败,这时候会导致整个插入流程卡壳。可以给这部分加个单独的try-catch,跳过有问题的文章:

// 在获取articleContent的地方改成这样
try {
    url = new URL(articleURL);
    urlConnection = (HttpsURLConnection) url.openConnection();
    // 允许重定向
    urlConnection.setInstanceFollowRedirects(true);
    in = urlConnection.getInputStream();
    // 指定UTF-8编码,避免乱码
    reader = new InputStreamReader(in, "UTF-8");
    data = reader.read();
    String articleContent = "";
    while (data!= -1){
        char current = (char) data;
        articleContent += current;
        data = reader.read();
    }
    // 插入数据库的代码
    String sql = "INSERT INTO articles(articleID,title,content) VALUES(? , ? , ?)";
    SQLiteStatement statement = articleDB.compileStatement(sql);
    statement.bindString(1,articleId);
    statement.bindString(2,articleTitle);
    statement.bindString(3,articleContent);
    statement.execute();
} catch (Exception e) {
    Log.e("ArticleContentError", "Failed to fetch content for article ID: " + articleId, e);
    continue; // 跳过当前文章,继续处理下一个
}

5. 数据库事务的可靠性

删除旧数据后插入新数据时,建议用事务包裹,确保要么全部插入成功,要么全部回滚,避免数据库里只有部分数据:

// 在doInBackground里的DELETE语句之后添加
articleDB.beginTransaction();
try {
    // 把你的for循环插入代码放在这里
    for (int i=0;i<numberOfItems;i++ ){
        // ... 原来的文章请求和插入代码 ...
    }
    articleDB.setTransactionSuccessful();
} finally {
    articleDB.endTransaction();
}

排查顺序建议

  1. 先加网络权限,重新运行App,看ListView有没有内容;
  2. 如果还是空的,看Logcat里的DB_CHECK日志,确认数据库有没有数据;
  3. 如果数据库没数据,看DownloadError日志,定位是API请求还是JSON解析出了问题;
  4. 最后优化容错和事务,提升稳定性。

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.14 06:49:39