我创建了一个AsyncTask类,我在其中编写了这个部分(onPreExecute和doInBackgrond方法):
@Override
protected void onPreExecute() {
// Instantiate ProgressDialog and Set style to STYLE_HORIZONTAL
dialog = new ProgressDialog(context);
dialog.setIndeterminate(false);
dialog.setMax(100);
dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
dialog.setCancelable(false);
dialog.setTitle("Update");
dialog.setMessage("Download data. Please wait...");
dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(String... urls) {
int count = 0;
int numUsers = 0;
db = new Database(context);
SQLiteDatabase localDB = db.getWritableDatabase();
try {
JSONArray UserJsonArray = connectAndCreateJsonArray(urls[0]);
numUsers = UserJsonArray.length();
localDB.beginTransaction();
for (int i = 0; i < numUsers; i++) {
JSONObject jObj = (JSONObject) UserJsonArray.getJSONObject(i);
localDB.insert("Users", null, getParsedUserEntry(jObj));
count++;
publishProgress(count * 100 / numUsers);
}
localDB.setTransactionSuccessful();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.e("Msg", "catch");
} finally {
localDB.endTransaction();
}
return numUsers+"";
}
private JSONArray connectAndCreateJsonArray(String url) {
JSONArray jsonArray = new JSONArray();
HttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("http://www.example.com/file.json");
request.addHeader("Cache-Control", "no-cache");
try {
HttpResponse response = client.execute(request);
HttpEntity entity = response.getEntity();
InputStreamReader in = new InputStreamReader(entity.getContent());
BufferedReader reader = new BufferedReader(in);
StringBuilder stringBuilder = new StringBuilder();
String line = "";
while ((line=reader.readLine()) != null) {
stringBuilder.append(line);
}
jsonArray = new JSONArray(stringBuilder.toString());
} catch (SocketTimeoutException e) {
e.printStackTrace();
} catch (SocketException e) {
e.printStackTrace();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (ClientProtocolException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
}
return jsonArray;
}
我一直都很好,但是今天在修改了其他类之后,从Android SDK Manager安装了2个软件包后,它就停止工作了。 (我将Android SDK Build-Tools v20和Android SDK Tools安装到版本23.0.5)。
现在我收到此错误: java.lang.IllegalStateException:无法执行此操作,因为没有当前事务
这是我的LogCat:
10-13 02:11:36.700: E/AndroidRuntime(31210): FATAL EXCEPTION: AsyncTask #1
10-13 02:11:36.700: E/AndroidRuntime(31210): Process: com.example.myapp, PID: 31210
10-13 02:11:36.700: E/AndroidRuntime(31210): java.lang.RuntimeException: An error occured while executing doInBackground()
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.os.AsyncTask$3.done(AsyncTask.java:300)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:355)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.FutureTask.setException(FutureTask.java:222)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.FutureTask.run(FutureTask.java:242)
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:231)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1112)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:587)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.lang.Thread.run(Thread.java:841)
10-13 02:11:36.700: E/AndroidRuntime(31210): Caused by: java.lang.IllegalStateException: Cannot perform this operation because there is no current transaction.
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.database.sqlite.SQLiteSession.throwIfNoTransaction(SQLiteSession.java:915)
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.database.sqlite.SQLiteSession.endTransaction(SQLiteSession.java:398)
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.database.sqlite.SQLiteDatabase.endTransaction(SQLiteDatabase.java:522)
10-13 02:11:36.700: E/AndroidRuntime(31210): at com.example.myapp.DownloadDataFromServer.doInBackground(DownloadDataFromServer.java:114)
10-13 02:11:36.700: E/AndroidRuntime(31210): at com.example.myapp.DownloadDataFromServer.doInBackground(DownloadDataFromServer.java:1)
10-13 02:11:36.700: E/AndroidRuntime(31210): at android.os.AsyncTask$2.call(AsyncTask.java:288)
10-13 02:11:36.700: E/AndroidRuntime(31210): at java.util.concurrent.FutureTask.run(FutureTask.java:237)
10-13 02:11:36.700: E/AndroidRuntime(31210): ... 4 more
10-13 02:11:37.765: E/WindowManager(31210): android.view.WindowLeaked: Activity com.example.myapp.Main has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView{420f12e8 V.E..... R....... 0,0-480,322} that was originally added here
10-13 02:11:37.765: E/WindowManager(31210): at android.view.ViewRootImpl.<init>(ViewRootImpl.java:346)
10-13 02:11:37.765: E/WindowManager(31210): at android.view.WindowManagerGlobal.addView(WindowManagerGlobal.java:248)
10-13 02:11:37.765: E/WindowManager(31210): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:69)
10-13 02:11:37.765: E/WindowManager(31210): at android.app.Dialog.show(Dialog.java:286)
10-13 02:11:37.765: E/WindowManager(31210): at com.example.myapp.DownloadDataFromServer.onPreExecute(DownloadDataFromServer.java:49)
10-13 02:11:37.765: E/WindowManager(31210): at android.os.AsyncTask.executeOnExecutor(AsyncTask.java:587)
10-13 02:11:37.765: E/WindowManager(31210): at android.os.AsyncTask.execute(AsyncTask.java:535)
10-13 02:11:37.765: E/WindowManager(31210): at com.example.myapp.Main.onOptionsItemSelected(Main.java:151)
10-13 02:11:37.765: E/WindowManager(31210): at android.app.Activity.onMenuItemSelected(Activity.java:2600)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.policy.impl.PhoneWindow.onMenuItemSelected(PhoneWindow.java:1065)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.view.menu.MenuBuilder.dispatchMenuItemSelected(MenuBuilder.java:741)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.view.menu.MenuItemImpl.invoke(MenuItemImpl.java:152)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:884)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.view.menu.MenuBuilder.performItemAction(MenuBuilder.java:874)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.view.menu.MenuPopupHelper.onItemClick(MenuPopupHelper.java:177)
10-13 02:11:37.765: E/WindowManager(31210): at android.widget.AdapterView.performItemClick(AdapterView.java:298)
10-13 02:11:37.765: E/WindowManager(31210): at android.widget.AbsListView.performItemClick(AbsListView.java:1113)
10-13 02:11:37.765: E/WindowManager(31210): at android.widget.AbsListView$PerformClick.run(AbsListView.java:2911)
10-13 02:11:37.765: E/WindowManager(31210): at android.widget.AbsListView$3.run(AbsListView.java:3645)
10-13 02:11:37.765: E/WindowManager(31210): at android.os.Handler.handleCallback(Handler.java:733)
10-13 02:11:37.765: E/WindowManager(31210): at android.os.Handler.dispatchMessage(Handler.java:95)
10-13 02:11:37.765: E/WindowManager(31210): at android.os.Looper.loop(Looper.java:136)
10-13 02:11:37.765: E/WindowManager(31210): at android.app.ActivityThread.main(ActivityThread.java:5146)
10-13 02:11:37.765: E/WindowManager(31210): at java.lang.reflect.Method.invokeNative(Native Method)
10-13 02:11:37.765: E/WindowManager(31210): at java.lang.reflect.Method.invoke(Method.java:515)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:732)
10-13 02:11:37.765: E/WindowManager(31210): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:566)
10-13 02:11:37.765: E/WindowManager(31210): at dalvik.system.NativeStart.main(Native Method)
第49行有:
dialog.show();
在线114有这样的:
localDB.endTransaction();
这很奇怪......可能是什么问题?谢谢。
答案 0 :(得分:2)
你可能会在beginTransaction
之前点击例外..将finally
移到另一个try
区块内
try {
JSONArray UserJsonArray = connectAndCreateJsonArray(urls[0]);
numUsers = UserJsonArray.length();
if (numUsers < 1) return numUsers + "";
try {
localDB.beginTransaction();
for (int i = 0; i < numUsers; i++) {
JSONObject jObj = (JSONObject) UserJsonArray.getJSONObject(i);
localDB.insert("Users", null, getParsedUserEntry(jObj));
count++;
publishProgress(count * 100 / numUsers);
}
localDB.setTransactionSuccessful();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.e("Msg", "catch");
} catch (Exception e) { // everything else
e.printStackTrace();
} finally {
localDB.endTransaction();
}
} catch (Exception e) { // everything else
e.printStackTrace();
}
答案 1 :(得分:2)
怎么样
try{
...
} catch (Exception e) { // everything else
e.printStackTrace();
} finally {
if(localDB != null && localDB.inTransation()){
localDB.endTransaction();
}
}
而不是“...将最终移动到另一个try块内”?
答案 2 :(得分:0)
我有一个像你的错误,这是我的代码:
try {
String sql = "SELECT * FROM "+DB_TABLEDOWNLOAD;
Cursor cursor = db.rawQuery(sql, null);
//空双引号为原表没有的字段
String temp = "";
int existIconPath = cursor.getColumnIndex("iconPath");
int existAudioID = cursor.getColumnIndex("audioID");
if (existIconPath == -1 && existAudioID == -1){
temp = "url, downed,total,path,name, audioTitle, audioFileID,\"\",\"\"";
}else if (existIconPath == -1 && existAudioID != -1){//iconPath不存在
temp = "url, downed,total,path,name, audioTitle, audioFileID,\"\",audioID";
}else if (existIconPath != -1 && existAudioID == -1){//audioID不存在
temp = "url, downed,total,path,name, audioTitle, audioFileID,iconPath,\"\"";
}else {
return;
}
db.beginTransaction();
String tempTableName = "_temp_"+DB_TABLEDOWNLOAD;
String sqlCreateTemp = " ALTER TABLE "+DB_TABLEDOWNLOAD+" RENAME TO "+tempTableName+";";
db.execSQL(sqlCreateTemp);
final String TB_TESTPAPERINFO_CREATE = "Create TABLE IF NOT EXISTS "
+ DB_TABLEDOWNLOAD
+ "(url TEXT, downed TEXT,total TEXT,path TEXT,name TEXT, audioTitle TEXT, audioFileID TEXT,iconPath TEXT, audioID TEXT);";
db.execSQL(TB_TESTPAPERINFO_CREATE);
String sqlBackupData = "INSERT INTO "+DB_TABLEDOWNLOAD+" SELECT "+temp+" FROM "+tempTableName+";";
db.execSQL(sqlBackupData);
String sqlDrop = "DROP TABLE IF EXISTS '"+tempTableName+"';";
db.execSQL(sqlDrop);
db.setTransactionSuccessful();
} catch (Exception e) {
e.printStackTrace();
}finally{
db.endTransaction();
}
我在db.beginTransaction()
之前使用return,我的代码在beginTransaction
之前返回,但在endTransaction
中我finally
。如果您没有beginTransaction
和endTransaction
,则会出现例外情况。
答案 3 :(得分:0)
在您的代码中,如果在localDB.beginTransaction();
之前发生异常,则不会打开该事务,并且由于在您最终试图关闭它的finally块中,将抛出IllegalStateException
(交易尚未开始)。
替换此代码:
try {
JSONArray UserJsonArray = connectAndCreateJsonArray(urls[0]);
numUsers = UserJsonArray.length();
localDB.beginTransaction();
for (int i = 0; i < numUsers; i++) {
JSONObject jObj = (JSONObject) UserJsonArray.getJSONObject(i);
localDB.insert("Users", null, getParsedUserEntry(jObj));
count++;
publishProgress(count * 100 / numUsers);
}
localDB.setTransactionSuccessful();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.e("Msg", "catch");
} finally {
localDB.endTransaction();
}
关于此代码:
JSONArray UserJsonArray = connectAndCreateJsonArray(urls[0]);
numUsers = UserJsonArray.length();
localDB.beginTransaction();
try {
for (int i = 0; i < numUsers; i++) {
JSONObject jObj = (JSONObject) UserJsonArray.getJSONObject(i);
localDB.insert("Users", null, getParsedUserEntry(jObj));
count++;
publishProgress(count * 100 / numUsers);
}
localDB.setTransactionSuccessful();
} catch (IllegalStateException e) {
e.printStackTrace();
} catch (JSONException e) {
e.printStackTrace();
Log.e("Msg", "catch");
} finally {
localDB.endTransaction();
}
如有必要,可以将前两行包装在单独的try-catch
中。