2011-02-02 3 views
2
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): Releasing statement in a finalizer. Please ensure that you explicitly call close() on your cursor: SELECT * FROM 
02-02 14:31:34.048: WARN/SQLiteCompiledSql(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
02-02 14:31:34.129: ERROR/Database(359): android.database.sqlite.DatabaseObjectNotClosedException: Application did not close the cursor or database object that was opened here 
02-02 14:31:34.129: ERROR/Database(359):  at 

이 예외를 피하는 방법 ?? Pls는db 닫기 및 커서 예외를 방지하는 방법

이 내 코드는 아래와 같습니다 도움 :

**DataSQLHelper .class** 

public class DataSQLHelper extends SQLiteOpenHelper { 
private static final String DATABASE_NAME = "test.db"; 
private static final int DATABASE_VERSION = 1; 

    public DataSQLHelper (Context context) { 
super(context, DATABASE_NAME, null, DATABASE_VERSION); 
} 



@Override 
public void onCreate(SQLiteDatabase db) { 
    String sql = "create table " + TABLE + "(" + BaseColumns._ID 
    + " integer primary key autoincrement, " + ID + " text, " 
    + PASSWORD + " text, " + ACTIVE + " text, " + STATUS 
    + " text);"; 
    db.execSQL(sql); 

} 

@Override 
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { 
if (oldVersion >= newVersion) 
    return; 

String sql = null; 
if (oldVersion == 1) 
    sql = "alter table " + TABLE + " add note text;"; 
if (oldVersion == 2) 
    sql = ""; 


if (sql != null) 
    db.execSQL(sql); 
} 

     @Override 
    public synchronized void close() { 
        super.close(); 


    } 

} 


    ***Test_Java .java*** 

    public class Test_Java extends Activity { 
    DataSQLHelper helData; 
SQLiteDatabase db; 
Cursor cursor; 


@Override 
public void onCreate(Bundle savedInstanceState) { 

super.onCreate(savedInstanceState); 
    setContentView(R.layout.main); 

    try {  

     helData= new DataSQLHelper(this); 
    cursor = getData(); 
    startManagingCursor(cursor); 

    setContentView(R.layout.view); 


} catch (Exception ex) { 

    } 

    } // onCreate Ends 



private Cursor getData() { 
try { 
    db = helData.getReadableDatabase(); 
    cursor = db.query(DataSQLHelper.TABLE, null, null, 
     null, null, null, null); 
    startManagingCursor(cursor); 
    return cursor; 
} catch (Exception ex) { 
    System.out.println("Exception Occured : " + ex.toString()); 
    return null; 
} 

} 

@Override 
protected void onDestroy() { 
    System.out.println("onDestroy"); 
    super.onDestroy(); 
    if (db!=null){ 
        db.close(); 
       } 
    if (cursor!=null){ 
        cursor.close(); 
    } 

    if (helData!=null){ 
     helData.close(); 
    } 

} 
} 

답변

0

예외가 해결되었습니다. 내가) (예외가 내가이 HEIKO 같은 준

4

당신은 당신들의 OnDestroy() 메소드 내에서 가까운 문을 반전합니다. 먼저 다음의 DB를 커서를 닫습니다

if (cursor!=null){ 
    cursor.close(); 
} 
if (db!=null){ 
    db.close(); 
} 

당신은 기본적으로 생성/DB 커서를 여는 순서를 반대로해야합니다.

예에서 db/cursor는 시스템 콜백 인 onCreate()에서 열립니다. 해당 메소드를 종료하기 전에 커서/db를 닫을 수 있습니다. 응용 프로그램에서 DataSQLHelper를 자유롭게 캐시 할 수 있습니다.

내 응용 프로그램에서는 Zwitscher으로 SQLHelper 클래스의 메서드로 전체 db/커서 처리를 처리하여 위의 레이어가 신경 쓸 필요가 없도록했습니다. TweetDB 클래스를 참조하십시오.

+0

를 던진 이유를 두 번

db = eventsData.getReadableDatabase(); 

이잖아 호출 ..하지만 여전히 같은 예외를 받고 ... 그리고 선은 방법 GetData의 내부되었다; db = helData.getReadableDatabase(); & cursor = db.query (DataSQLHelper.TABLE, null, null, null, null, null, null); – MorningGlory

+0

이것은 내 코드입니다. protected void onDestroy() { super.onDestroy(); if (cursor! = null) { cursor.close(); } if (db! = null) { db.close(); } } – MorningGlory

+0

메신저 같은 예외를 받고 ... Pls는 도움말 – jennifer

관련 문제