在 Postexecute 方法中使用 GridView 的 SearchView

SearchView with GridView in the Postexecute Method

在你们复制它之前,我已经到处搜索它,但我被困在需要你们专业知识的地方。

我在 doInBackground 方法中调用我的数据并将值存储在 SimpleAdapter 中,在 postExecute 方法中,我将适配器分配给我的 gridView。而且在 PostExecute 中我有一个 searchView 方法来过滤我的适配器。

现在,当我在搜索中按任何字母时,我的应用程序因 NullPointerException 而崩溃,如果我 运行 在同一线程上使用相同的代码而没有创建 AsyncTask,我不会收到此错误。这是我的 DoInBackground 代码:

protected Void doInBackground(Void... voids) {

        try{
            connect = CONN(un, passwords, db, ip);
            String StoredProc = "{call pro.DocumentDelivery_inst_update_delete(?,?)}";
            callableStatement=connect.prepareCall(StoredProc);
            callableStatement.setString(1, "Select");
            callableStatement.setString(2, MainActivity.usernam);
            rs = callableStatement.executeQuery();

            while(rs.next()){
                Map<String,String> datanum = new HashMap<String, String>();
                datanum.put("B",rs.getString("FullName"));
                datanum.put("C",rs.getString("Name"));
                datanum.put("D",rs.getString("DocumentType"));
                datanum.put("A",rs.getString("Id"));
                datanum.put("E",rs.getString("TaskType"));
                datanum.put("F",rs.getString("Comments"));
                datanum.put("G",rs.getString("Tasktime"));
                datanum.put("H",rs.getString("Priority"));
                datanum.put("I",rs.getString("Telephone"));
                data.add(datanum);
            }

            String [] from = {"A","B","C","D","E","F","G","H","I"};
            int[] views = {R.id.doc_grid_t4,R.id.doc_grid_t1, R.id.doc_grid_t2,R.id.doc_grid_t3,R.id.doc_grid_t5,R.id.doc_grid_t6,R.id.doc_grid_t7,R.id.doc_grid_t8,R.id.doc_grid_t9};
            ADA = new SimpleAdapter(document_Pro_List.this,data,R.layout.custom_pro_document_view, from, views);


            synchronized (this) {
                int counter = 0;
                while(counter<=4){
                    this.wait(100);
                    counter++;
                    publishProgress(counter*25);
                }
            }
        }
        catch (InterruptedException | SQLException e){
            e.printStackTrace();
        }
        return null;

    }

这是我的 postExecute 方法代码:

protected void onPostExecute(Void result){
        sv = (SearchView) findViewById(R.id.searchView);
        progressDialog.dismiss();
        gridView.setAdapter(ADA);
        sv.setOnQueryTextListener(new SearchView.OnQueryTextListener() {

            @Override
            public boolean onQueryTextSubmit(String query) {
                return false;
            }

            @Override
            public boolean onQueryTextChange(String query) {
                ADA.getFilter().filter(query);
                //ADA.notifyDataSetChanged();
                return false;
            }
        });

}

注意:我取出这个的那一刻

Map<String,String> datanum = new HashMap<String, String>();

在 while 循环之外,我不再收到 NullPointerException 但这对我没有帮助,因为它随后将数据库中的第一个数据重复到所有下一个数据。 我也知道我应该使用 WebAPI,而且我正在并肩进行。 请帮助我解决如何删除此异常的问题。

谢谢你

所以我想通了,有时我的

中有一个空值
datanum.put("D",rs.getString("DocumentType"));

因为这变成了 Null,searchView 无法处理那个 Null 值并给出了 NullPointerException。