android.database.staledataexception:访问关闭的游标

时间:2011-09-07 07:51:10

标签: android eclipse sqlite

我在我的活动中使用自定义列表视图并试图用值填充它。但是,我得到了这个“staledataexception”,我不知道为什么......

以下是代码,错误发生在标记为“error here”

的行上
protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        setContentView(R.layout.upadtemailservr);

        headText = (TextView)findViewById(R.id.TextView01);
        prefsPrivate = getSharedPreferences(PREFS_PRIVATE,Context.MODE_PRIVATE);
        Heading = prefsPrivate.getString(KEY_PRIVATE, "the string not found");
        headText.setText("Update Mail Server - "+Heading);

        AddNew = (Button)findViewById(R.id.Button01);
        AddNew.setOnClickListener(this);


         m_orders = new ArrayList<Order>();

        //the first tried one... this was not working... so i tried the one writtten next to it.
        /*
         m_orders = new ArrayList<Order>();
         this.m_adapter = new OrderAdapter(this, R.layout.row, m_orders);
         this.setListAdapter(this.m_adapter);
            */      

        //new list adapter

        MainList = (ListView)findViewById(R.id.mainlist1);


           viewOrders = new Runnable()
           {

            @Override
            public void run() {
                // TODO Auto-generated method stub
                getOrders();
            }
           };
             Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
                thread.start();
                m_ProgressDialog = ProgressDialog.show(UpdateMailServr.this,    
                      "Please wait...", "Retrieving data ...", true);

                m_adapter = new OrderAdapter(this,R.layout.row,m_orders);
               MainList.setAdapter(m_adapter);
               MainList.setOnItemClickListener(this);

    }  

    private Runnable returnRes = new Runnable(){

         @Override
            public void run() {
                if(m_orders != null && m_orders.size() > 0){
                    m_adapter.notifyDataSetChanged();
                    for(int i=0;i<m_orders.size();i++)
                    m_adapter.add(m_orders.get(i));
                }
                m_ProgressDialog.dismiss();
                m_adapter.notifyDataSetChanged();
            }
    };



           public void getOrders() {
               m_orders = new ArrayList<Order>();

               adapter1 = new DBAdaptertrial2(this);
            adapter1.open();
            Cursor cur1 = adapter1.fetchAllMeetings();

            cur1.moveToFirst();
            for(int i=0;i<cur1.getCount();i++)
            {
                         //******Error here**************
                if(Heading.equalsIgnoreCase(cur1.getString(1)))
                {
                    try
                    {
                        if(!cur1.getString(5).equals(""))
                        {
                            Order o1 = new Order();
                            o1.setMailserver(cur1.getString(5));
                            o1.setPriority(cur1.getInt(8));
                            o1.setIpaddr(cur1.getString(2));
                            m_orders.add(o1);
                        }
                        if(!cur1.getString(6).equals(""))
                        {
                            Order o2 = new Order();
                            o2.setMailserver(cur1.getString(6));
                            o2.setPriority(cur1.getInt(8));
                            o2.setIpaddr(cur1.getString(2));
                            m_orders.add(o2);
                        }
                        if(!cur1.getString(7).equals(""))
                        {
                            Order o3 = new Order();
                            o3.setMailserver(cur1.getString(6));
                            o3.setPriority(cur1.getInt(8));
                            o3.setIpaddr(cur1.getString(2));
                            m_orders.add(o3);
                        }
                        Thread.sleep(3000);
                        Log.i("ARRAY", ""+ m_orders.size());

                    }
                    catch (Exception e) {
                        Log.e("BACKGROUND_PROC", e.getMessage());
                    }
                  runOnUiThread(returnRes);

                }
                else
                {
                    cur1.moveToNext();
                }   
                cur1.close();
            adapter1.close();


            }

    }       

           @Override
        protected void onResume() {
            // TODO Auto-generated method stub
            super.onResume();

               viewOrders = new Runnable()
               {

                @Override
                public void run() {
                    // TODO Auto-generated method stub
                    getOrders();
                }
               };
                 Thread thread =  new Thread(null, viewOrders, "MagentoBackground");
                    thread.start();
                    m_ProgressDialog = ProgressDialog.show(UpdateMailServr.this,    
                          "Please wait...", "Retrieving data ...", true);

           }  

        // End of all the list stuff

1 个答案:

答案 0 :(得分:5)

您似乎正在使用managedQuery获取游标。

我在恢复应用程序时遇到了问题,由于某种原因,Icecream Sandwich没有正确关闭游标。修复是在使用它们之后立即对所有游标执行此操作:

if (cursor != null && !cursor.isClosed()) {
  myActivity.stopManagingCursor( cursor );
  cursor.close();
}