我想在数据库第一次加载数据时实现进度对话框。
这是我的代码
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
mDatabase.execSQL(ENG_FRA_TABLE_CREATE);
loadDictionaryEngFra();
new InitDB().execute();
}
public class InitDB extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(mHelperContext, "", "Wait", true);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
如果我尝试使用此进度对话框运行应用程序,则会抛出此错误列表
04-18 23:16:30.886: E/SQLiteLog(26059): (1) no such table: ENG_FRAdictionary
04-18 23:16:30.896: E/SQLiteDatabase(26059): Error inserting suggest_text_1=abbey suggest_text_2=n. a monastery ruled by an abbot
04-18 23:16:30.896: E/SQLiteDatabase(26059): android.database.sqlite.SQLiteException: no such table: ENG_FRAdictionary (code 1): , while compiling: INSERT INTO ENG_FRAdictionary(suggest_text_1,suggest_text_2) VALUES (?,?)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:686)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1573)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.addWordEngFra(DictionaryDatabaseEngFra.java:238)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.loadWordsEngFra(DictionaryDatabaseEngFra.java:218)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.access$1(DictionaryDatabaseEngFra.java:208)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper$1.run(DictionaryDatabaseEngFra.java:200)
04-18 23:16:30.896: E/SQLiteDatabase(26059): at java.lang.Thread.run(Thread.java:856)
04-18 23:16:30.896: E/DictionaryDatabaseEngFra(26059): unable to add word: abbey
04-18 23:16:30.896: W/dalvikvm(26059): threadid=14: thread exiting with uncaught exception (group=0x41d24930)
04-18 23:16:30.896: E/AndroidRuntime(26059): FATAL EXCEPTION: Thread-51326
04-18 23:16:30.896: E/AndroidRuntime(26059): java.lang.IllegalStateException: attempt to re-open an already-closed object: SQLiteDatabase: /data/data/com.example.searchtoto/databases/dictionaryEngFra
04-18 23:16:30.896: E/AndroidRuntime(26059): at android.database.sqlite.SQLiteClosable.acquireReference(SQLiteClosable.java:55)
04-18 23:16:30.896: E/AndroidRuntime(26059): at android.database.sqlite.SQLiteDatabase.insertWithOnConflict(SQLiteDatabase.java:1543)
04-18 23:16:30.896: E/AndroidRuntime(26059): at android.database.sqlite.SQLiteDatabase.insert(SQLiteDatabase.java:1445)
04-18 23:16:30.896: E/AndroidRuntime(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.addWordEngFra(DictionaryDatabaseEngFra.java:238)
04-18 23:16:30.896: E/AndroidRuntime(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.loadWordsEngFra(DictionaryDatabaseEngFra.java:218)
04-18 23:16:30.896: E/AndroidRuntime(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.access$1(DictionaryDatabaseEngFra.java:208)
04-18 23:16:30.896: E/AndroidRuntime(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper$1.run(DictionaryDatabaseEngFra.java:200)
04-18 23:16:30.896: E/AndroidRuntime(26059): at java.lang.Thread.run(Thread.java:856)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): Search suggestions query threw an exception.
04-18 23:16:30.996: W/SuggestionsAdapter(26059): android.view.WindowManager$BadTokenException: Unable to add window -- token null is not for an application
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.view.ViewRootImpl.setView(ViewRootImpl.java:804)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:265)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:73)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.app.Dialog.show(Dialog.java:282)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.app.ProgressDialog.show(ProgressDialog.java:116)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.app.ProgressDialog.show(ProgressDialog.java:99)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper$InitDB.onPreExecute(DictionaryDatabaseEngFra.java:174)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:586)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.os.AsyncTask.execute(AsyncTask.java:534)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra$DictionaryOpenHelper.onCreate(DictionaryDatabaseEngFra.java:162)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.database.sqlite.SQLiteOpenHelper.getReadableDatabase(SQLiteOpenHelper.java:188)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra.query(DictionaryDatabaseEngFra.java:113)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryDatabaseEngFra.getWordMatchesEngFra(DictionaryDatabaseEngFra.java:96)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryProviderEngFra.getSuggestions(DictionaryProviderEngFra.java:107)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at com.example.searchtoto.dictionary.en.DictionaryProviderEngFra.query(DictionaryProviderEngFra.java:81)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.content.ContentProvider.query(ContentProvider.java:652)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.content.ContentProvider$Transport.query(ContentProvider.java:189)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.content.ContentResolver.query(ContentResolver.java:375)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.content.ContentResolver.query(ContentResolver.java:318)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.app.SearchManager.getSuggestions(SearchManager.java:930)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.widget.SuggestionsAdapter.runQueryOnBackgroundThread(SuggestionsAdapter.java:200)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.widget.CursorFilter.performFiltering(CursorFilter.java:49)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.widget.Filter$RequestHandler.handleMessage(Filter.java:234)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.os.Handler.dispatchMessage(Handler.java:99)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.os.Looper.loop(Looper.java:137)
04-18 23:16:30.996: W/SuggestionsAdapter(26059): at android.os.HandlerThread.run(HandlerThread.java:60)
执行进度对话框,执行app运行正常
这是活动
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.HashMap;
import android.app.ProgressDialog;
import android.app.SearchManager;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.database.sqlite.SQLiteQueryBuilder;
import android.os.AsyncTask;
import android.os.Environment;
import android.provider.BaseColumns;
import android.text.TextUtils;
import android.util.Log;
/**
* Contains logic to return specific words from the dictionary, and
* load the dictionary table when it needs to be created.
*/
public class DictionaryDatabaseEngFra extends SearchableDictionaryEngFra{
private static final String TAG = "DictionaryDatabaseEngFra";
//The columns we'll include in the dictionary table
public static final String KEY_WORD_ENG_FRA = SearchManager.SUGGEST_COLUMN_TEXT_1;
public static final String KEY_DEFINITION_ENG_FRA = SearchManager.SUGGEST_COLUMN_TEXT_2;
private static final String DATABASE_NAME = "dictionaryEngFra";
private static final String ENG_FRA_TABLE = "ENG_FRAdictionary";
private static final int DATABASE_VERSION = 2;
private final DictionaryOpenHelper mDatabaseOpenHelper;
private static final HashMap<String,String> mColumnMapEngFra = buildColumnMapEngFra();
static File ENG_FRA = new File(Environment.getExternalStorageDirectory().getPath() + "/dic/dictionary/engfra./", "eng_fra.txt");
/**
* Constructor
* @param context The Context within which to work, used to create the DB
*/
public DictionaryDatabaseEngFra(Context context) {
mDatabaseOpenHelper = new DictionaryOpenHelper(context);
}
/**
* Builds a map for all columns that may be requested, which will be given to the
* SQLiteQueryBuilder. This is a good way to define aliases for column names, but must include
* all columns, even if the value is the key. This allows the ContentProvider to request
* columns w/o the need to know real column names and create the alias itself.
*/
private static HashMap<String,String> buildColumnMapEngFra() {
HashMap<String,String> mapEngFra = new HashMap<String,String>();
mapEngFra.put(KEY_WORD_ENG_FRA, KEY_WORD_ENG_FRA);
mapEngFra.put(KEY_DEFINITION_ENG_FRA, KEY_DEFINITION_ENG_FRA);
mapEngFra.put(BaseColumns._ID, "rowid AS " +
BaseColumns._ID);
mapEngFra.put(SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_INTENT_DATA_ID);
mapEngFra.put(SearchManager.SUGGEST_COLUMN_SHORTCUT_ID, "rowid AS " +
SearchManager.SUGGEST_COLUMN_SHORTCUT_ID);
return mapEngFra;
}
public Cursor getWordEngFra(String rowId, String[] columns) {
String selection = "rowid = ?";
String[] selectionArgs = new String[] {rowId};
return query(selection, selectionArgs, columns);
}
public Cursor getWordMatchesEngFra(String query, String[] columns) {
String selection = KEY_WORD_ENG_FRA + " MATCH ?";
String[] selectionArgs = new String[] {query+"*"};
return query(selection, selectionArgs, columns);
}
private Cursor query(String selection, String[] selectionArgs, String[] columns) {
/* The SQLiteBuilder provides a map for all possible columns requested to
* actual columns in the database, creating a simple column alias mechanism
* by which the ContentProvider does not need to know the real column names
*/
SQLiteQueryBuilder builder = new SQLiteQueryBuilder();
builder.setTables(ENG_FRA_TABLE);
builder.setProjectionMap(mColumnMapEngFra);
Cursor cursor = builder.query(mDatabaseOpenHelper.getReadableDatabase(),
columns, selection, selectionArgs, null, null, null);
if (cursor == null) {
return null;
} else if (!cursor.moveToFirst()) {
cursor.close();
return null;
}
return cursor;
}
/**
* This creates/opens the database.
*/
private static class DictionaryOpenHelper extends SQLiteOpenHelper {
private final Context mHelperContext;
private SQLiteDatabase mDatabase;
/* Note that FTS3 does not support column constraints and thus, you cannot
* declare a primary key. However, "rowid" is automatically used as a unique
* identifier, so when making requests, we will use "_id" as an alias for "rowid"
*/
private static final String ENG_FRA_TABLE_CREATE =
"CREATE VIRTUAL TABLE " + ENG_FRA_TABLE +
" USING fts3 (" +
KEY_WORD_ENG_FRA + ", " +
KEY_DEFINITION_ENG_FRA + ");";
DictionaryOpenHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
mHelperContext = context;
}
@Override
public void onCreate(SQLiteDatabase db) {
mDatabase = db;
mDatabase.execSQL(ENG_FRA_TABLE_CREATE);
loadDictionaryEngFra();
new InitDB().execute();
}
public class InitDB extends AsyncTask<Void, Void, Void> {
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = ProgressDialog.show(mHelperContext, "", "Wait", true);
}
@Override
protected Void doInBackground(Void... params) {
return null;
}
@Override
protected void onPostExecute(Void result) {
super.onPostExecute(result);
pDialog.dismiss();
}
}
/**
* Starts a thread to load the database table with words
*/
private void loadDictionaryEngFra() {
new Thread(new Runnable() {
public void run() {
try {
loadWordsEngFra();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
}).start();
}
private void loadWordsEngFra() throws IOException {
Log.d(TAG, "Loading words eng_fra...");
FileInputStream fileInputStream = new FileInputStream(ENG_FRA);
BufferedReader reader = new BufferedReader(new InputStreamReader(fileInputStream));
try {
String line;
while ((line = reader.readLine()) != null) {
String[] strings = TextUtils.split(line, "-");
if (strings.length < 2) continue;
long id = addWordEngFra(strings[0].trim(), strings[1].trim());
if (id < 0) {
Log.e(TAG, "unable to add word: " + strings[0].trim());
}
}
} finally {
reader.close();
}
Log.d(TAG, "DONE loading words.");
}
/**
* Add a word to the dictionary.
* @return rowId or -1 if failed
*/
public long addWordEngFra(String word, String definition) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_WORD_ENG_FRA, word);
initialValues.put(KEY_DEFINITION_ENG_FRA, definition);
return mDatabase.insert(ENG_FRA_TABLE, null, initialValues);
}
/**
* Add a word to the dictionary.
* @return rowId or -1 if failed
*/
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
+ newVersion + ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS " + ENG_FRA_TABLE);
onCreate(db);
}
}
}
答案 0 :(得分:0)
这看起来像是一个SQL错误。
04-18 23:16:30.896: E/SQLiteDatabase(26059): Error inserting suggest_text_1=abbey suggest_text_2=n. a monastery ruled by an abbot
04-18 23:16:30.896: E/SQLiteDatabase(26059): android.database.sqlite.SQLiteException: no such table: ENG_FRAdictionary (code 1): , while compiling: INSERT INTO ENG_FRAdictionary(suggest_text_1,suggest_text_2) VALUES (?,?)
看起来你的ENG_FRAdictionary表不存在。
答案 1 :(得分:0)
问题出在您的sqlite数据库中。我看不到进度对话框的问题 检查数据库创建语句和表名称。另请检查sqlitehelper类 让我们知道会发生什么