2013-02-13 2 views
0

현재 데이터베이스에서 ListView를 업데이트하는 메서드를 호출 할 때 내 앱에서 부실 개체 예외가 발생합니다.ListView 메서드가 '부실 개체 상태'예외를 throw하는 이유는 무엇입니까?

데이터베이스를 쿼리 할 때 모든 커서를 닫을 때주의를 기울였으며 'startManagingCursor'를 사용하여 반환 될 때도 커서를 관리하지만 여전히이 문제가 발생합니다.

현재 데이터베이스에 새 항목을 추가 할 때 클래스의 개인 메서드 내에서 ListView 업데이트 메서드를 호출합니다. 이것은 객체가 예외를 던지는 곳입니다.

나는 사람들의 상쾌한 세션을 읽었으며, 또한 데이터베이스 버전이 이와 같다고 생각한다. 하지만 전반적으로 나는 갇혀 있고 이것을 해결할 수 없습니다.

업데이트 오류 :

02-13 13 : 23 : 06.588 : E/AndroidRuntime (287) FATAL EXCEPTION : E/AndroidRuntime (287) : 23 : 06.588 02-13 13 주 로이드. 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) : 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) : com.android.internal.app.AlertController $ ButtonHandler.handleMessage (AlertController.java:158) 02-13 13 : 23 : 06.588 : E/AndroidRuntime (287) : android.os.Handler.dispatchMessage (Handler.java:99) 02-13 13 : 23 : 06.588 : E/AndroidRuntime (287) : android.os.Looper. 루프 (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) : java.lang.reflect.Method.invokeNative (네이티브 메소드) 02-13 13 : 23 : 06.588 : E/AndroidRuntime (287) : java.la ng.reflect.Method.invoke (Method.java:521) 02-13 13 : 23 : 06.588 : E/AndroidRuntime (287) : 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) :

:이 데이터베이스에서 반환되는 커서입니다

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(); 

} 
} 

: dalvik.system.NativeStart.main (기본 방법)에

이리스트 뷰를 호출하는 내 클래스

+0

아마도 커서를 닫았 기 때문에'접근 닫힌 커서'-> – njzk2

답변

0

02-13 11:37:52.013: E/AndroidRuntime(287): android.database.StaleDataException: Access closed cursor

닫힌 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(); 
    } 
} 
+0

답변에 많은 감사드립니다. onDestroy 및 closeSources 메서드에 대한 질문입니다. 내 메인 클래스 또는 데이터베이스 처리기 클래스 내에서 이러한 메서드를 구현해야합니까? – user1352057

+0

은 Activity 클래스에 있습니다. :) 이러한 메서드 (onDestroy 또는 onPause 및 next ...)는 활동 라이프 사이클의 메서드이기 때문에 :)) – Sajmon

+0

답변을 많이 보내 주셔서 다시 감사드립니다. 나는이 방법들을 구현할 것이다. 지금은 내 DB 처리기에서 c.close() 함수를 제거했지만 이제 내 데이터베이스에 추가 할 때 내 'show()'메서드에서 충돌이 발생합니까? – user1352057

관련 문제