Android: Can't create handler that has not called Looper.prepare() on AsynTask and Database activities

Let me start by saying that I've seeing that this problem and all the possible solution but I cant understand how it applies here so I have no idea where to start. My MainActiviy checks if this is the first time the user started the application and fill the database. The onCreate on the SQL Helper creates jut the tables. If this is the first time it runs, I invoke an AsyncTask that checks the language and then fill the database with the correct data. Everything works except that I get this exception when I try to insert something in the database. I can't see where is the handler that is messing with the code. Any clues would be great! Stack is this: 07-16 19:49:06.854: ERROR/DatabaseCreatorTask(10725): Error trying to insert categories:Can't create handler inside thread that has not called Looper.prepare() 07-16 19:49:06.854: WARN/System.err(10725): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 07-16 19:49:06.854: WARN/System.err(10725): at android.os.Handler.(Handler.java:121) 07-16 19:49:06.854: WARN/System.err(10725): at android.app.Activity.(Activity.java:679) 07-16 19:49:06.854: WARN/System.err(10725): at com.objects.Category.(Category.java:19) 07-16 19:49:06.854: WARN/System.err(10725): at tasks.DatabaseCreatorTask.fillCategoriesTable(DatabaseCreatorTask.java:58) 07-16 19:49:06.854: WARN/System.err(10725): at tasks.DatabaseCreatorTask.doInBackground(DatabaseCreatorTask.java:89) 07-16 19:49:06.854: WARN/System.err(10725): at tasks.DatabaseCreatorTask.doInBackground(DatabaseCreatorTask.java:1) 07-16 19:49:06.854: WARN/System.err(10725): at android.os.AsyncTask$2.call(AsyncTask.java:185) 07-16 19:49:06.854: WARN/System.err(10725): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:305) 07-16 19:49:06.854: WARN/System.err(10725): at java.util.concurrent.FutureTask.run(FutureTask.java:137) 07-16 19:49:06.854: WARN/System.err(10725): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1068) 07-16 19:49:06.854: WARN/System.err(10725): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:561) 07-16 19:49:06.854: WARN/System.err(10725): at java.lang.Thread.run(Thread.java:1096) This is part of my source code: doInBackground of the AsyncTask: protected Boolean doInBackground(Void ... strings) { Log.i(LOG_TAG, "doInBackground..."); String lang = PreferencesHelper.getInstance().getSettings(context).getString("lang", "en"); boolean resultCategories = fillCategoriesTable(lang); return (resultCategories); } And this if the method that throws the exception: private boolean fillCategoriesTable(String lang) { boolean result = true; Log.i(LOG_TAG, "Filling categories..."); if (lang != null && lang.length() > 0) { Log.i(LOG_TAG, "Context: " + context); Log.i(LOG_TAG, "Language: " + lang); if (lang.equalsIgnoreCase("es")) { try { Category cat1 = new Category("Entretenimiento", null); Log.i(LOG_TAG, "Creating new category Entretenimiento:" + cat1.create(this.context)); Category cat2 = new Category("Viajes", null); Log.i(LOG_TAG, "Creating new category Viajes:" + cat2.create(this.context)); Category cat3 = new Category("Comidas", null); Log.i(LOG_TAG, "Creating new category Comidas:" + cat3.create(this.context)); } catch (Exception e) { Log.e(LOG_TAG, "Error trying to insert categories: " + e.getMessage()); e.printStackTrace(); result = false; } } else { try { Category cat1 = new Category("Entertainment", null); Log.i(LOG_TAG, "Creating new category Entertainment:" + cat1.create(this.context)); Category cat2 = new Category("Travel", null); Log.i(LOG_TAG, "Creating new category Travel:" + cat2.create(this.context)); Category cat3 = new Category("Dining", null); Log.i(LOG_TAG, "Creating new category Dining:" + cat3.create(this.context)); } catch (Exception e) { result = false; Log.e(LOG_TAG, "Error trying to insert categories:" + e.getMessage()); e.printStackTrace(); } } } else { Log.w(LOG_TAG, "Lang is null"); result = false; } return result; }

以上就是Android: Can't create handler that has not called Looper.prepare() on AsynTask and Database activities的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏