Android:无法创建未在AsyncTask和数据库活动上调用Looper.prepare()的处理程序

时间:2011-07-16 23:35:13

标签: java android android-asynctask

首先我要说的是,我已经看到了这个问题以及所有可能的解决方案,但我无法理解它是如何应用的,所以我不知道从哪里开始。

我的MainActiviy检查这是否是用户第一次启动应用程序并填充数据库。 SQL Helper上的onCreate会创建表格。

如果这是第一次运行,我会调用AsyncTask检查语言,然后用正确的数据填充数据库。除了我尝试在数据库中插入内容时出现此异常,一切正常。我无法看到处理程序在哪里弄乱代码。

任何线索都会很棒!

Stack就是这样:

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;

}

1 个答案:

答案 0 :(得分:0)

您的Category构造函数似乎正在尝试创建ActivityCategory扩展Activity的实例。不要那样做。