【问题标题】:Failed to change locale for db to 'en_US'无法将 db 的语言环境更改为“en_US”
【发布时间】:2016-02-19 09:04:47
【问题描述】:

我浏览了有关此问题的所有链接,但无法获得任何线索。我得到的异常报告来自 Google Developer Console。我在 Nexus 4 设备中尝试了代码,它工作正常。

下面是我的堆栈跟踪和代码。谁能找到异常发生的原因。

堆栈跟踪

Fatal Exception: android.database.sqlite.SQLiteException: Failed to change locale for db '/data/data/in.plackal.lovecyclesfree/databases/LoveCycles.db' to 'en_US'.
       at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:399)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:224)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:199)
       at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
       at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
       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:1167)
       at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
       at in.plackal.lovecyclesfree.database.DatabaseOperations.open(DatabaseOperations.java:85)
       at in.plackal.lovecyclesfree.general.CycleManager.readSettings(CycleManager.java:7724)
       at in.plackal.lovecyclesfree.applicationwidget.WidgetTodayService.getValueFromCycleManager(WidgetTodayService.java:51)
       at in.plackal.lovecyclesfree.applicationwidget.WidgetTodayService.onHandleIntent(WidgetTodayService.java:37)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:211)
       at android.os.HandlerThread.run(HandlerThread.java:61)
Caused by android.database.sqlite.SQLiteDatabaseLockedException: database is locked (code 5)
       at android.database.sqlite.SQLiteConnection.nativeExecute(SQLiteConnection.java)
       at android.database.sqlite.SQLiteConnection.execute(SQLiteConnection.java:561)
       at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:390)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:224)
       at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:199)
       at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:463)
       at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:185)
       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:1167)
       at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:268)
       at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
       at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
       at in.plackal.lovecyclesfree.database.DatabaseOperations.open(DatabaseOperations.java:85)
       at in.plackal.lovecyclesfree.general.CycleManager.readSettings(CycleManager.java:7724)
       at in.plackal.lovecyclesfree.applicationwidget.WidgetTodayService.getValueFromCycleManager(WidgetTodayService.java:51)
       at in.plackal.lovecyclesfree.applicationwidget.WidgetTodayService.onHandleIntent(WidgetTodayService.java:37)
       at android.app.IntentService$ServiceHandler.handleMessage(IntentService.java:65)
       at android.os.Handler.dispatchMessage(Handler.java:102)
       at android.os.Looper.loop(Looper.java:211)
       at android.os.HandlerThread.run(HandlerThread.java:61)

使用的代码

public class DatabaseOperations
{
    private Context m_Context;

    private DataBaseWrapper dbHelper;

    private SQLiteDatabase database;

    public DatabaseOperations(Context context)
    {
        m_Context = context;
        dbHelper = new DataBaseWrapper(context);
    }

    public void open() throws SQLException
    {
        database = dbHelper.getWritableDatabase();
    }

    public void close()
    {
        dbHelper.close();
    }
}


import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class DataBaseWrapper extends SQLiteOpenHelper 
{
    // Database Name
    private static final String DATABASE_NAME = "TestDB.db";

    // Database Version
    private static final int DATABASE_VERSION = 2;


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

    @Override
    public void onCreate(SQLiteDatabase db) 
    {               
        db.execSQL(CREATE_CYCLE_TABLE);
        db.execSQL(CREATE_NOTES_TABLE);
            db.execSQL(CREATE_SETTINGS_TABLE);
        }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
    {

    }

    @Override
    public void onOpen(SQLiteDatabase db)
    {
        super.onOpen(db);
    }
}

【问题讨论】:

标签: java android sqlite


【解决方案1】:

在这篇文章中

Failed to change locale for db '/data/data/my.easymedi.controller/databases/EasyMediInfo.db' to 'en_US'

他们通过改变解决了这个问题

myDatabase = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READWRITE);

myDatabase = SQLiteDatabase.openDatabase
(myPath, null, SQLiteDatabase.NO_LOCALIZED_COLLATORS | SQLiteDatabase.OPEN_READWRITE);

但它对我不起作用,因为我正在使用 dbHelper.getReadableDatabase()

显然没有办法指定像NO_LOCALIZED_COLLATORSSQLiteOpenHelper 这样的标志,所以你必须扩展你自己版本的SQLiteOpenHelper 并在openDatabase() 方法中指定标志。

我的问题出现在 API23 平板电脑的 Android Studio 模拟器上,其他地方都没有。 我不知道这与提到的标志有什么关系,但在我增加模拟器的内存后,错误消失了。

我猜,Android 的工作方式很神秘。

【讨论】:

  • 就我而言,我使用的是 Android Room
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-12-15
  • 1970-01-01
  • 2021-08-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多