0

目前在我的应用程序中,当我调用从我的数据库中更新 ListView 的方法时,我遇到了一个过时的对象异常。

在查询我的数据库时,我一直小心地关闭所有游标,当通过使用“startManagingCursor”返回游标时,我也管理我的游标,但我仍然遇到这个问题。

目前,当我向我的数据库添加一个新项目时,我想起了我类的私有方法中的 ListView 更新方法。这是对象抛出异常的地方。

我读过人们“刷新”会话,而且我相信数据库版本会出现这种情况?但总的来说,我被卡住了,无法解决这个问题。

更新错误:

02-13 13:23:06.588: E/AndroidRuntime(287): 致命异常: main 02-13 13:23:06.588: E/AndroidRuntime(287): android.database.StaleDataException: 访问关闭游标 02-13 13: 23:06.588: E/AndroidRuntime(287): 在 android.database.AbstractWindowedCursor.checkPosition(AbstractWindowedCursor.java:217) 02-13 13:23:06.588: E/AndroidRuntime(287): 在 android.database.AbstractWindowedCursor.getInt (AbstractWindowedCursor.java:84) 02-13 13:23:06.588: E/AndroidRuntime(287): at com.example.flybase2.ShoppingList.updateTotal(ShoppingList.java:248) 02-13 13:23:06.588: E /AndroidRuntime(287): 在 com.example.flybase2.ShoppingList.access$1(ShoppingList.java:231) 02-13 13:23:06.588: E/AndroidRuntime(287): 在 com.example.flybase2.ShoppingList$2。 onClick(ShoppingList.java:350) 02-13 13:23:06.588: E/AndroidRuntime(287): at com.android。internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158) 02-13 13:23:06.588: E/AndroidRuntime(287): at android.os.Handler.dispatchMessage(Handler.java:99) 02-13 13:23:06.588: E/AndroidRuntime(287): 在 android.os.Looper.loop(Looper.java:123) 02-13 13:23:06.588: E/AndroidRuntime(287): 在 android.app.ActivityThread .main(ActivityThread.java:4627) 02-13 13:23:06.588: E/AndroidRuntime(287): at java.lang.reflect.Method.invokeNative(Native Method) 02-13 13:23:06.588: E/ AndroidRuntime(287): at java.lang.reflect.Method.invoke(Method.java:521) 02-13 13:23:06.588: E/AndroidRuntime(287): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller .run(ZygoteInit.java:868) 02-13 13:23:06.588: E/AndroidRuntime(287): 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626) 02-13 13:23 :06.588:E/AndroidRuntime(287): 在 dalvik.system.NativeStart.main(Native Method)

这是我调用 ListView 的类:

package com.example.flybase2;

import android.app.AlertDialog;
import android.app.AlertDialog.Builder;
import android.app.Dialog;
import android.app.ListActivity;
import android.content.DialogInterface;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.text.Editable;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

public class ShoppingList extends ListActivity implements OnClickListener {

Button AddItem;
ListView showItems;
SimpleCursorAdapter cursorAdapter;
Long itemId;
EditText totalPrice;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.shoppinglistlayout);


    AddItem = (Button) findViewById(R.id.btnAddItem);

    showItems = (ListView)findViewById(android.R.id.list);

    totalPrice = (EditText)findViewById(R.id.totalListPrice);

    AddItem.setOnClickListener(this);

    setList();

}
    ERROR**********************************8
@Override
public void onClick(View clickedAdd) {



    show();

}

@Override
protected void onListItemClick(ListView l, View v, int position, long idd) {
    super.onListItemClick(l, v, position, idd);

     itemId = idd;

final CharSequence[] items = {"Edit Item", "Delete Item", "Show Most Purchased Item"};

    Builder alertDialogBuilder = new AlertDialog.Builder(ShoppingList.this);

    alertDialogBuilder.setTitle("Item Options:");



    alertDialogBuilder.setItems(items, new DialogInterface.OnClickListener() {

        public void onClick(DialogInterface dialog, int item) {

            if (items[item].equals("Edit Item")) {

                AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);

                builder.setTitle("Edit Item");


                DBHandlerShop setEdit = new DBHandlerShop(ShoppingList.this, null, null);

                setEdit.open();
                String itemName = setEdit.getItem(itemId);
                int itemAmount = setEdit.getItemQuan(itemId);
                int itemPrice = setEdit.getItemCost(itemId);
                setEdit.close();
                                    LinearLayout layout = new LinearLayout(ShoppingList.this);
                                    layout.setOrientation(LinearLayout.VERTICAL);

                                    final EditText titleBox = new EditText(ShoppingList.this);
                                    titleBox.setText(itemName);
                                    titleBox.setHint("Item Name:");
                                    layout.addView(titleBox);

                                    final EditText quantityBox = new EditText(ShoppingList.this);
                                    quantityBox.setText(Integer.toString(itemAmount));
                                    quantityBox.setHint("Item Quantity");
                                    layout.addView(quantityBox);

                                    final EditText priceBox = new EditText(ShoppingList.this);
                                    priceBox.setText(Integer.toString(itemPrice));
                                    priceBox.setHint("Item Price.");
                                    layout.addView(priceBox);

                                    builder.setView(layout);



                                    builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog, int whichButton) {


                                        Editable valueItem = titleBox.getText();
                                        Editable valueAmount = quantityBox.getText();
                                    Editable valuePrice = priceBox.getText();

                                   String itemDescription = valueItem.toString();
                                    String s = valueAmount.toString();
                                    int itemAmount = Integer.parseInt(s);
                                    String a = valuePrice.toString();
                                    int itemPrice = Integer.parseInt(a);
                                    try{
                                                DBHandlerShop update = new DBHandlerShop(ShoppingList.this, null, null);
                                                update.open();
                                                update.updateItem(itemId, itemDescription, itemAmount, itemPrice);
                                                update.close();
                                    }
                                    catch(Exception e)
                                    {
                                        Dialog e1 = new Dialog(ShoppingList.this);
                                        e1.setTitle("Item unsuccesfully updated");
                                        TextView txt = new TextView(ShoppingList.this);
                                        txt.setText("Success");
                                        e1.setContentView(txt);
                                        e1.show();
                                    }
                                    finally
                                    {
                                        Dialog e1 = new Dialog(ShoppingList.this);
                                        e1.setTitle("Item succesfully updated");
                                        TextView txt = new TextView(ShoppingList.this);
                                        txt.setText("Success");
                                        e1.setContentView(txt);
                                        e1.show();

                                        setList();

                                    int cost = updateTotal();
                                        totalPrice.setText(cost);
                                    }


                                      }


                                    });

                                    builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                                      public void onClick(DialogInterface dialog, int whichButton) {

                                      }
                                    });

                                    builder.show();


            }

            else if (items[item].equals("Delete Item")) 
            {

                   DBHandlerShop delete = new DBHandlerShop(ShoppingList.this,null, null);

                   delete.open();
                   delete.deleteItem(itemId);
                   delete.close();

                   setList();

            }

            else if (items[item].equals("Show Most Purchased Item")) 
            {

            }

            }  

        });

    alertDialogBuilder.show();

}


private void setList() {

    DBHandlerShop DBShop = new DBHandlerShop(this, null, null);

    DBHandlerShop searchItems = new DBHandlerShop(this, null, null);

    searchItems.open();

    Cursor cursor = searchItems.getItems();

    startManagingCursor(cursor);

    String [] from = new String [] {DBShop.KEY_ITEMSHOP, DBShop.KEY_ITEMNUM, DBShop.KEY_ITEMPRICE};
    int [] to = new int [] {R.id.txtSetItem, R.id.txtSetAmount, R.id.txtSetPrice};

    cursorAdapter = new SimpleCursorAdapter(this, R.layout.setshoppinglist, cursor, from, to);
    showItems.setAdapter(cursorAdapter);


}


private int updateTotal() {


    DBHandlerShop total = new DBHandlerShop(this, null, null);

    int totalPrice = 0;
    total.open();
    Cursor totalPrices = total.getTotals();
    total.close();

     if (totalPrices != null) {

         startManagingCursor(totalPrices);
         if (totalPrices.moveToFirst()) {


             do {
                 int cost = totalPrices.getInt(3);
                 totalPrice += cost;

             } while (totalPrices.moveToNext());

             return totalPrice;
         }

     }

    return 0;


}

private void show()
{

    AlertDialog.Builder builder = new AlertDialog.Builder(ShoppingList.this);

        builder.setTitle("Enter Item Details:");

                            LinearLayout layout = new LinearLayout(this);
                            layout.setOrientation(LinearLayout.VERTICAL);

                            final EditText titleBox = new EditText(this);

                            titleBox.setHint("Item Name:");
                            layout.addView(titleBox);

                            final EditText quantityBox = new EditText(this);

                            quantityBox.setHint("Item Quantity");
                            layout.addView(quantityBox);

                            final EditText priceBox = new EditText(this);

                            priceBox.setHint("Item Price.");
                            layout.addView(priceBox);

                            builder.setView(layout);



                            builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                            public void onClick(DialogInterface dialog, int whichButton) {
                            try
                            {

                                Editable valueItem = titleBox.getText();
                                Editable valueAmount = quantityBox.getText();
                            Editable valuePrice = priceBox.getText();


                                String itemDescription = valueItem.toString();
                                String s = valueAmount.toString();
                                int itemAmount = Integer.parseInt(s);
                                String a = valuePrice.toString();
                                int itemPrice = Integer.parseInt(a);




                            DBHandlerShop addItem = new DBHandlerShop(ShoppingList.this, null, null);
                            addItem.open();
                            addItem.insertItems(itemDescription, itemAmount, itemPrice);
                            addItem.close();


                            }
                            catch(Exception e)
                            {
                            Dialog e1 = new Dialog(ShoppingList.this);
                            e1.setTitle("Item unsuccesfully added");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e1.setContentView(txt);
                            e1.show();

                            }
                            finally
                            {
                                Dialog e = new Dialog(ShoppingList.this);
                            e.setTitle("Item succesfully added.");
                            TextView txt = new TextView(ShoppingList.this);
                            txt.setText("Success");
                            e.setContentView(txt);
                            e.show();

                        setList();

                    int cost = updateTotal();
                        totalPrice.setText(cost);

                            }


                              }

                            });

                            builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
                              public void onClick(DialogInterface dialog, int whichButton) {

                              }
                            });

                            builder.show();

}
}

这是从数据库返回的游标:

    public Cursor getItems() {


        String [] columns = new String[]{KEY_ROWSHOPID, KEY_ITEMSHOP, KEY_ITEMNUM, KEY_ITEMPRICE};
        Cursor c = ourDatabase.query(DATABASE_TABLESHOP, columns, null, null, null, null, null);
         if(c != null && c.getCount() > 0)
        {
        c.moveToFirst();
        c.close();
        return c;
        }
        c.close();
        return null;
    }
4

1 回答 1

0

02-13 11:37:52.013: E/AndroidRuntime(287): android.database.StaleDataException: 访问关闭游标

您的问题是您正在尝试访问已关闭的数据Cursor。这是不允许的,因此删除行,c.close()现在它应该可以工作了。

注意:一般建议关闭任何游标、数据源onPause()onDestroy()方法。

更新: 这里是编辑过的方法,复制粘贴再试一次。它应该有效。

public Cursor getItems() {
   String [] columns = new String[] {KEY_ROWSHOPID, KEY_ITEMSHOP, KEY_ITEMNUM, 
                                     KEY_ITEMPRICE};
   Cursor c = ourDatabase.query(DATABASE_TABLESHOP, columns, null, null, null,
                                null, null);
   if (c.moveToFirst()) {
      return c;
   }
   return null;
}


public void onDestroy() {
   super.onDestroy();
   closeSources();
}

private void closeSources() {
   if (c != null) {
      c.close();
   }
   if (db != null) {
      db.close();
   }
}
于 2013-02-13T12:10:07.353 回答