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;
}
Your
Categoryconstructor appears to be attempting to create an instance of anActivity, or perhapsCategoryextendsActivity. Don’t do that.