在模拟器上启动应用程序时出错显示位置已停止

时间:2016-11-11 22:33:30

标签: java android sqlite

我收到以下错误:

11-12 03:49:31.085 2207-2217/com.google.android.gms E/StrictMode: A resource was acquired at attached stack trace but never released. See java.io.Closeable for information on avoiding resource leaks.
                                                                  java.lang.Throwable: Explicit termination method 'close' not called
                                                                      at dalvik.system.CloseGuard.open(CloseGuard.java:184)
                                                                      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:190)
                                                                      at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:177)
                                                                      at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:806)
                                                                      at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:791)
                                                                      at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:694)
                                                                      at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1142)
                                                                      at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:1131)
                                                                      at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:261)
                                                                      at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:261)
                                                                      at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:261)
                                                                      at ivm.g(:com.google.android.gms:204)
                                                                      at ivm.e(:com.google.android.gms:176)
                                                                      at ivh.h(:com.google.android.gms:27519)
                                                                      at ong.b(:com.google.android.gms:277)
                                                                      at com.google.android.gms.googlehelp.service.ClearHelpHistoryChimeraIntentOperation.onHandleIntent(:com.google.android.gms:36)
                                                                      at com.google.android.chimera.IntentOperation.onHandleIntent(:com.google.android.gms:95)
                                                                      at bie.run(:com.google.android.gms:1873)
                                                                      at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
                                                                      at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
                                                                      at java.lang.Thread.run(Thread.java:818)

这是我的代码:

package com.newboston.location;
import android.content.ContentResolver;
import android.database.CharArrayBuffer;
import android.database.ContentObserver;
import android.database.DataSetObserver;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.Cursor;
import android.content.Context;
import android.content.ContentValues;
import android.net.Uri;
import android.os.Bundle;
public class MyDBHandler extends SQLiteOpenHelper {
  private static final int DATABASE_VERSION = 1;
  private static final String DATABASE_NAME = "DETAILS.db";
  private static final String TABLE_DETAILS = "details";
  private static final String COLUMN_ID = "_id";
  private static final String COLUMN_NAME = "_name";
  private static final String COLUMN_PHONE = "_phone_no";

  public MyDBHandler(Context context, String name, SQLiteDatabase.CursorFactory factory, int version) {
    super(context, DATABASE_NAME, factory, DATABASE_VERSION);
  }

  @Override
  public void onCreate(SQLiteDatabase sqLiteDatabase) {
    String query = "CREATE TABELE " + TABLE_DETAILS + "(" +
            COLUMN_ID + " INTEGER PRIMARY KEY AUTOINCREMENT " +
            COLUMN_NAME + " TEXT " +
            COLUMN_PHONE + " TEXT " +
            ");";
    sqLiteDatabase.execSQL(query);
  }

  @Override
  public void onUpgrade(SQLiteDatabase sqLiteDatabase, int i, int i1) {
    sqLiteDatabase.execSQL("DROP TABLE IF EXISTS " + TABLE_DETAILS);
    onCreate(sqLiteDatabase);
  }

  public void addDetails(Details details) {
    ContentValues values = new ContentValues();
    values.put(COLUMN_NAME, details.get_name());
    SQLiteDatabase db = getWritableDatabase();
    db.insert(TABLE_DETAILS, null, values);
    db.close();
  }

  public void deleteDetails(String name) {
    SQLiteDatabase db = getWritableDatabase();
    db.execSQL("DELETE FROM " + TABLE_DETAILS + " WHERE " + COLUMN_NAME + " =\"" + name + "\";");
    db.close();


  }

  public String printDetails() {
    String dbString = "";
    SQLiteDatabase db = getWritableDatabase();
    String query = "SELECT * FROM " + TABLE_DETAILS + " WHERE 1";
    Cursor c = db.rawQuery(query, null);
    c.moveToFirst();
    while(!(c.isAfterLast()))
    {
        if(c.getString(c.getColumnIndex("name"))!=null)
        {
            dbString+=c.getString(c.getColumnIndex("name"));
            dbString+="\n";
        }
         c.moveToNext();
    }
    db.close();
    c.close();
    return dbString;
  }
}

我无法找到我没有关闭任何资源的地方,虽然我已经关闭了我打开的所有数据库。但是log cat显示仍然是错误的。

2 个答案:

答案 0 :(得分:0)

您可能在更新之前已在模拟器上安装了以前版本的应用程序,我建议您首先从模拟器清除应用程序或从模拟器中完全卸载应用程序,然后才能继续。

答案 1 :(得分:0)

我会将此作为评论添加,但我没有足够的声誉。

您是否在清单中添加了适当的权限。 如果已经存在,请从模拟器中卸载应用程序并尝试重新运行应用程序。

检查此查询1是什么?

String query = "SELECT * FROM " + TABLE_DETAILS + " WHERE 1";