使用异步任务将数据显示到列表视图时强制关闭应用程序

Posted

技术标签:

【中文标题】使用异步任务将数据显示到列表视图时强制关闭应用程序【英文标题】:Force Close App when display data into listview using asynctask 【发布时间】:2018-03-01 04:14:15 【问题描述】:

当我运行 applicatiom 时被强制关闭。此应用程序功能是使用 asynctask 在 listview 中显示来自 sqlite 的数据。但我在

处收到空指针错误

collegeList.add(地图); --> 空指针

然后,我想要的是,数据将按 id 显示。 id 是外键。

这是我的代码:

private class GetHttpResponse extends AsyncTask<Void, Void, Void> 
    ArrayList<HashMap<String, String>> collegeList;
    protected void onPreExecute()
     super.onPreExecute();

    protected Void doInBackground(Void... arg0) 
        List<Rainday> raindaydetail = new LinkedList<Rainday>();
        raindaydetail=dbHelper.getAllRainday();

        for(Rainday temp_vg: raindaydetail)
            temp_vg.getId();
            temp_vg.getYear_rainday();
            temp_vg.getJan();
            temp_vg.getFeb();
            temp_vg.getMar();
            temp_vg.getApr();
            temp_vg.getMay();
            temp_vg.getJun();
            temp_vg.getJul();
            temp_vg.getAug();
            temp_vg.getSep();
            temp_vg.getOct();
            temp_vg.getNov();
            temp_vg.getDec();

            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();
            // adding each child node to HashMap key => value
            map.put("id", String.valueOf(temp_vg.getId()));
            map.put("year_rainfall", temp_vg.getYear_rainday());
            map.put("jan", temp_vg.getJan());
            map.put("feb", temp_vg.getFeb());
            map.put("mar", temp_vg.getMar());
            map.put("apr", temp_vg.getApr());
            map.put("may", temp_vg.getMay());
            map.put("jun", temp_vg.getJun());
            map.put("jul", temp_vg.getJul());
            map.put("aug", temp_vg.getAug());
            map.put("sep", temp_vg.getSep());
            map.put("oct", temp_vg.getOct());
            map.put("nov", temp_vg.getNov());
            map.put("dec", temp_vg.getDec());

          collegeList.add(map);
    

    return null;



protected void onPostExecute(Void result)


    proCollageList.setVisibility(View.GONE);
    listCollege.setVisibility(View.VISIBLE);
    if (collegeList != null) 
        ListAdapterRainday adapter = new ListAdapterRainday(RaindayReport.this, collegeList);
        listCollege.setAdapter(adapter);

mysqliteHelper:

 public List<Rainday> getAllRainday() 
    List<Rainday> rainday = new LinkedList<Rainday>();
    // 1. build the query
    String query = "SELECT  * FROM " + TABLE_RAINDAY + " WHERE id = " + KEY_ID_RAINDAY;
    // 2. get reference to writable DB
    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(query, null);
    // 3. go over each row, build book and add it to list
    Rainday rainday1 = null;
    if (cursor.moveToFirst()) 
        do 
            rainday1 = new Rainday();
            rainday1.setId(Integer.parseInt(cursor.getString(0)));
            rainday1.setYear_rainday(cursor.getString(1));
            rainday1.setJan(cursor.getString(2));
            rainday1.setFeb(cursor.getString(3));
            rainday1.setMar(cursor.getString(4));
            rainday1.setApr(cursor.getString(5));
            rainday1.setMay(cursor.getString(6));
            rainday1.setJun(cursor.getString(7));
            rainday1.setJul(cursor.getString(8));
            rainday1.setAug(cursor.getString(9));
            rainday1.setSep(cursor.getString(10));
            rainday1.setOct(cursor.getString(11));
            rainday1.setNov(cursor.getString(12));
            rainday1.setDec(cursor.getString(13));
            // Add vgm
            rainday.add(rainday1);
         while (cursor.moveToNext());
    
    return rainday;

我需要你的帮助。如果有人帮助我,我将不胜感激。

【问题讨论】:

它只是一个空指针.. 查找为什么您的列表为空。你初始化了吗? 【参考方案1】:

我想你忘了初始化你的 collegeList

像这样初始化它

ArrayList<HashMap<String, String>> collegeList;
        collegeList= new ArrayList<>();

【讨论】:

【参考方案2】:

在你的代码中,我在下面一行找到了

ArrayList<HashMap<String, String>> collegeList;

而且您没有在代码中的任何位置初始化collegeList

【讨论】:

以上是关于使用异步任务将数据显示到列表视图时强制关闭应用程序的主要内容,如果未能解决你的问题,请参考以下文章

如何显示用户将 Android 应用程序安装到列表视图? [关闭]

列表视图

使用异步任务时光标变为空?

在异步任务运行时显示警报框并在任务完成时删除?

休眠“在视图中打开会话”和异步任务

音频android确实强制关闭应用程序