Android:无法创建未在 AsynTask 和数据库活动上调用 Looper.prepare() 的处理程序
首先我要说的是,我已经看到了这个问题和所有可能的解决方案,但我无法理解它在这里如何应用,所以我不知道从哪里开始。
我的 MainActiviy 检查这是否是用户第一次启动应用程序并填充数据库。 SQL Helper 上的 onCreate 仅创建表。
如果这是第一次运行,我会调用一个 AsyncTask 来检查语言,然后用正确的数据填充数据库。一切正常,除了当我尝试在数据库中插入某些内容时出现此异常。我看不到扰乱代码的处理程序在哪里。
任何线索都会很棒!
堆栈是这样的:
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.<init>(Handler.java:121)
07-16 19:49:06.854: WARN/System.err(10725): at android.app.Activity.<init>(Activity.java:679)
07-16 19:49:06.854: WARN/System.err(10725): at com.objects.Category.<init>(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)
这是我的源代码的一部分:
AsyncTask 的 doInBackground:
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);
}
如果抛出异常的方法是:
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;
}
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.<init>(Handler.java:121)
07-16 19:49:06.854: WARN/System.err(10725): at android.app.Activity.<init>(Activity.java:679)
07-16 19:49:06.854: WARN/System.err(10725): at com.objects.Category.<init>(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;
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您的
Category
构造函数似乎正在尝试创建Activity
的实例,或者Category
扩展了Activity
。不要那样做。Your
Category
constructor appears to be attempting to create an instance of anActivity
, or perhapsCategory
extendsActivity
. Don't do that.