当我尝试使用SQLiteOpenHelper获取可写(可读)数据库时,我收到错误:
E/SQLiteDatabase: Failed to open database '/data/user/0/com.smt/databases/SMT'.
android.database.sqlite.SQLiteException: not an error (code 0)
at android.database.sqlite.SQLiteConnection.nativeRegisterLocalizedCollators(Native Method)
at android.database.sqlite.SQLiteConnection.setLocaleFromConfiguration(SQLiteConnection.java:361)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:218)
at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:193)
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:835)
at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:820)
at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:723)
at android.app.ContextImpl.openOrCreateDatabase(ContextImpl.java:669)
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:290)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:223)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:163)
at com.smt.model.data.db.DatabaseHelper.<init>(DatabaseHelper.java:48)
at com.smt.model.data.db.SmtDatabase.<init>(SmtDatabase.java:48)
at com.smt.presenter.impl.CatalogPresenterImpl.loadCatalogForParentId(CatalogPresenterImpl.java:40)
at com.smt.view.fragment.CatalogFragment.onCreateView(CatalogFragment.java:61)
at android.support.v4.app.Fragment.performCreateView(Fragment.java:2354)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1419)
at android.support.v4.app.FragmentManagerImpl.moveFragmentToExpectedState(FragmentManager.java:1740)
at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1809)
at android.support.v4.app.BackStackRecord.executeOps(BackStackRecord.java:799)
at android.support.v4.app.FragmentManagerImpl.executeOps(FragmentManager.java:2580)
at android.support.v4.app.FragmentManagerImpl.executeOpsTogether(FragmentManager.java:2367)
at android.support.v4.app.FragmentManagerImpl.removeRedundantOperationsAndExecute(FragmentManager.java:2322)
at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:2229)
at android.support.v4.app.FragmentManagerImpl.dispatchStateChange(FragmentManager.java:3221)
at android.support.v4.app.FragmentManagerImpl.dispatchActivityCreated(FragmentManager.java:3171)
at android.support.v4.app.FragmentController.dispatchActivityCreated(FragmentController.java:192)
at android.support.v4.app.FragmentActivity.onStart(FragmentActivity.java:560)
at android.support.v7.app.AppCompatActivity.onStart(AppCompatActivity.java:177)
at android.app.Instrumentation.callActivityOnStart(Instrumentation.java:1322)
at android.app.Activity.performStart(Activity.java:6777)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2696)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2819)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1532)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6321)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
这是我的 SQLiteOpenHelper
的 DatabaseHelper.java :
public class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(SQL_CREATE_CATEGORIES);
db.execSQL(SQL_CREATE_MATERIALS);
db.execSQL(SQL_CREATE_LOCALIZATIONS);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL(SQL_DROP_CATEGORIES);
db.execSQL(SQL_DROP_LOCALIZATIONS);
db.execSQL(SQL_DROP_MATERIALS);
onCreate(db);
}
}
方法 onCreate 或 onUpgrade 未调用。
的 SmtDatabase.java:
public class SmtDatabase {
private static final String TAG = SmtDatabase.class.getName();
private Context mContext;
private SQLiteDatabase mDatabase;
public SmtDatabase(Context context) {
DatabaseHelper dbHelper = new DatabaseHelper(context);
mDatabase = dbHelper.getWritableDatabase(); // <------ Here is error
this.mContext = context;
}
/* SELECT, QUERY, UPDATE Methods */
}
我的清单:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.smt">
<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<!-- After there are application with activity definitions -->
</manifest>
我还要求在运行时读写外部存储器。在带有android 7.1.1的设备和带有android 6.0的模拟器上测试。两者都有错误。但是android 4.4的模拟器效果很好!!!
我无法理解我做错了什么。昨天这段代码就像一个魅力,但今天我总是得到“不是错误的SQLite异常”。
答案 0 :(得分:2)
我试图解决这个问题2天,但没有结果。今天我创建了新项目,然后复制/粘贴所有类,这是一种治疗方法。
UDP :我发现为什么会出现这个问题。当我在 Application.java 类中使用下面的代码更改语言时,我输入了不正确的字符串值(“русский”),配置变得无法正确创建数据库。
@Override
public void onCreate() {
super.onCreate();
Fabric.with(this, new Crashlytics());
PreferenceManager mPreferenceManager = new PreferenceManager(getApplicationContext());
mLang = mPreferenceManager.getLocale(); // Here I have got incorrect value
if (mLang.equals("default")) mLang = getResources().getConfiguration().locale.getLanguage();
Log.d(TAG, "onCreate: lang = " + mLang);
mLocale = new Locale(mLang);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config, null);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mLocale = new Locale(mLang);
Locale.setDefault(mLocale);
Configuration config = new Configuration();
config.locale = mLocale;
getBaseContext().getResources().updateConfiguration(config, null);
}