gpt4 book ai didi

android - java.lang.IllegalStateException : Cannot perform this operation because there is no current transaction 错误

转载 作者:太空狗 更新时间:2023-10-29 16:36:57 25 4
gpt4 key购买 nike

我创建了一个 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 管理器安装了 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();

这很奇怪……可能是什么问题?谢谢。

最佳答案

关于

try{
...
} catch (Exception e) { // everything else
e.printStackTrace();
} finally {
if(localDB != null && localDB.inTransation()){
localDB.endTransaction();
}
}

而不是“...将 finally 移动到内部的另一个 try block ”?

关于android - java.lang.IllegalStateException : Cannot perform this operation because there is no current transaction 错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26331362/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com