- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我创建了一个 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/
在 Tomcat 6/Ubuntu 12.04 上启动 Grails 2.1.0 应用程序时出现以下错误。 Error 500 - Internal Server Error. groovy.lang
在运行 Storm 拓扑时,我收到此错误。拓扑完美运行 5 分钟,没有任何错误,然后失败。我正在使用 Config.TOPOLOGY_TICK_TUPLE_FREQ_SECS as 300 sec i
我有一个 jsp 代码在其中一台机器上运行良好。但是当我复制到另一台机器时,我得到了这个 no such method found 异常。我是 Spring 的新手。有人可以解释我错过了什么吗? 以下
已关闭。此问题需要 debugging details 。目前不接受答案。 编辑问题以包含 desired behavior, a specific problem or error, and the
我的代码在下面给出了一个错误; Exception in thread "main" java.lang.NoSuchMethodError: com/myApp/Client.cypherCBC(L
我正在尝试一个 Restful web 服务示例,所以当我要访问 url 时,我遇到了异常 java.lang.NoSuchMethodError: jersey.repackaged.com.goo
我正在将一个 Spring web 项目转换为一个 Maven 项目,但我收到了这个错误: java.lang.NoSuchMethodError: org.jboss.logging.Logger.
在我的项目中,我有一个像这样的枚举: public enum MyEnum { FIRST(1), SECOND(2); private int value; private MyEnum(int v
我创建了这个简单的示例,用于读取 Linux 正常运行时间: public String getMachineUptime() throws IOException { String[] di
我正在使用 Eclipse,并且正在使用 Java。我的目标是使用 bogoSort 方法对 vector 进行排序在一个 vector (vectorExample)中适应我的 vector 类型,
我正在运行以下查询。它显示一条错误消息。如何解决这个错误? ListrouteList=null; List companyList = session.createS
我有以下模型类: @Entity @Table(name="user_content") @org.hibernate.annotations.NamedQueries({ @org.
我有那个错误。这是我的代码: GmailSettingsService service = new GmailSettingsService(APPLICATION_NAME, DOMAIN_NAME
实际上我在执行我的java程序时遇到了下面提到的错误 Exception in thread "pool-1-thread-1" java.lang.ClassCastException: jav
java.lang.ClassCastException: java.lang.Float cannot be cast to java.lang.String 我在以下代码中遇到此异常: Strin
我正在尝试从 linkedhashset 中检索随机元素。下面是我的代码,但它每次都给我异常。 private static void generateRandomUserId(Set userIds
我已经完成了 Android 中的代码: List spinnerArray = new ArrayList(); for (int i = 0; i item = (LinkedTreeMap)
这个问题已经有答案了: Explanation of ClassCastException in Java (12 个回答) 已关闭 6 年前。 我已经编写了 java 到 Json 的代码,同时从页
这个问题在这里已经有了答案: ClassCastException java.lang.Long cannot be cast to clojure.lang.IFn (4 个答案) 关闭 6 年前
我在运行时遇到问题来编译这段代码,这给我一个错误,java.lang.Integer 无法转换为 Java.lang.Double。如果有人帮助我更正此代码,我将非常高兴 double x; pu
我是一名优秀的程序员,十分优秀!