无法使用AsyncTask在尚未调用Looper.prepare()的线程内创建处理程序 [英] Can't create handler inside thread that has not called Looper.prepare() with AsyncTask

查看:110
本文介绍了无法使用AsyncTask在尚未调用Looper.prepare()的线程内创建处理程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在android上使用AsyncTask类,但是我遇到了问题.

I am trying to use the AsyncTask class on android but I have a problem.

这是我的代码:

 public void CheckIfUserIsLogedIn ()
{
    int userPresence = localDatabase.CheckUserPresence();
    if (userPresence == 0) 
    {
        setContentView(R.layout.main);
        new checkCountryAsync().execute();
    }
    else
    {
        setContentView(R.layout.userlogedon);
    }
}  

class checkCountryAsync extends AsyncTask<String, String, String> {

    @Override
    protected void onPreExecute() {
        super.onPreExecute(); 
    }
    @Override
    protected String doInBackground(String... aurl) {
        int countryPresence = localDatabase.CheckCountryPresence();
        if (countryPresence == 0) 
        {
            CountryTable country = new CountryTable() ;
            country.EnterCountry();
        }
        return null;
    }
}

我不知道我在哪里做错了.LocalDatabase类中的CheckCountryPresence()方法仅进行查询并返回结果.

I don't know where I am doing something wrong. The method CheckCountryPresence() from the LocalDatabase class is only making a query and returns a result.

谢谢.

这是logCat:

02-20 08:41:25.804: W/dalvikvm(26458): threadid=10: thread exiting with uncaught exception (group=0x4001d5a0)
02-20 08:41:25.834: E/AndroidRuntime(26458): FATAL EXCEPTION: AsyncTask #1
02-20 08:41:25.834: E/AndroidRuntime(26458): java.lang.RuntimeException: An error occured while executing doInBackground()
02-20 08:41:25.834: E/AndroidRuntime(26458):    at android.os.AsyncTask$3.done(AsyncTask.java:200)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.FutureTask.run(FutureTask.java:138)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.lang.Thread.run(Thread.java:1027)
02-20 08:41:25.834: E/AndroidRuntime(26458): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
02-20 08:41:25.834: E/AndroidRuntime(26458):    at android.os.Handler.<init>(Handler.java:121)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at android.app.Activity.<init>(Activity.java:717)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at milos.mdpi.CountryTable.<init>(CountryTable.java:5)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at milos.mdpi.MDPIActivity$checkCountryAsync.doInBackground(MDPIActivity.java:367)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at milos.mdpi.MDPIActivity$checkCountryAsync.doInBackground(MDPIActivity.java:1)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at android.os.AsyncTask$2.call(AsyncTask.java:185)
02-20 08:41:25.834: E/AndroidRuntime(26458):    at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
02-20 08:41:25.834: E/AndroidRuntime(26458):    ... 4 more

推荐答案

似乎您正在做一些事情,以通过 Non-UI 线程对UI进行 Update .因此,您应该将自己的内容放在 runOnUiThread()中以更新UI,以便它在您的UI自身上执行内容.

Seems you are doing some stuff to Update your UI from a Non-UI thread. So, you should put your stuff to update the UI inside runOnUiThread() so that it executes the stuff on your UI hread itself.

更新:

只需尝试将您的东西放入 onPreExecute()中的 CheckCountryPresence(); 中即可.因此,您编辑的班级应该像

Just try to put your stuff for getting CheckCountryPresence(); inside onPreExecute(). So your edited class should be like,

class checkCountryAsync extends AsyncTask<String, String, String> {

    int countryPresence = 0;
    @Override
    protected void onPreExecute() {
       countryPresence = localDatabase.CheckCountryPresence();
    }
    @Override
    protected String doInBackground(String... aurl) {

        Activity_name.this.runOnUiThread(new Runnable() {

        @Override
        public void run() {
            if (countryPresence == 0) 
            {
              CountryTable country = new CountryTable() ;
              country.EnterCountry();
            }
        }
    });
        return null;
    }
}

这篇关于无法使用AsyncTask在尚未调用Looper.prepare()的线程内创建处理程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆