多线程UI:exceptionininitializererror和运行时异常android

时间:2013-02-10 13:17:32

标签: android multithreading android-asynctask runtimeexception

我正在尝试进行多线程处理,但我在几部智能手机(SGS2)上都有这种堆栈跟踪:

java.lang.ExceptionInInitializerError
at com.android.bordeaux.code.model.AnnouncersContainer.setLoudArrayFromJSON_online(AnnouncersContainer.java:68)
at com.android.bordeaux.code.SplashscreenActivity_Second$1.run(SplashscreenActivity_Second.java:55)
Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
at android.os.Handler.<init>(Handler.java:121)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask$InternalHandler.<init>(AsyncTask.java:421)
at android.os.AsyncTask.<clinit>(AsyncTask.java:152)
... 2 more 

这是我的主要活动(splashscreen),它等待几秒钟等待我的asynctask结束:

    public class SplashscreenActivity_Second extends Activity 
        {       
            @Override
            public void onCreate(Bundle savedInstanceState) 
            {
                super.onCreate(savedInstanceState);
                setContentView(R.layout.splashscreen_second);

                // thread
                Thread splashThread = new Thread()
                {
                    @Override
                    public void run()
                    {
                        try
                        {
                            int waited = 0;

                            if (isInternetOn())
                            {
// Here I'm making my asynctask !!
AnnouncersContainer.setLoudArrayFromJSON_online(getApplicationContext());
                                while (waited < 5000)
                                {
                                    sleep(50);
                                    waited += 40;
                                }
                            }
                            else
                            {                       
                                AnnouncersContainer.setLoudArrayFromXML_local(getApplicationContext());
                                while(waited < 5000)
                                {
                                    sleep(50);
                                    waited += 60;
                                }
                            }
                        }
                        catch( InterruptedException e )
                        {
                            e.printStackTrace();
                        }
                        finally
                        {
                            Intent intent_to_tabhost = new Intent(SplashscreenActivity_Second.this, MyTabActivity.class);
                            startActivity(intent_to_tabhost);
                            finish();
                        }
                    }
                };
                splashThread.start();
            }
}

这是我的asynctask:

public class DownloadAnnouncers extends AsyncTask<Void, Integer, Boolean>
{
    public static Boolean loadFinished = false;

    //JSON variables..


    private static String url = null;

    Context context;

    public DownloadAnnouncers(Context context) 
    {
        this.context = context;
    }

    @Override
    protected void onPreExecute() 
    {
        super.onPreExecute();
    }

    protected Boolean doInBackground(Void... params) 
    {

    // fine retrieving all my JSON data in a global array..

    }

    @Override
    protected void onPostExecute(Boolean downloadedArray) 
    {
        super.onPostExecute(downloadedArray);
    }
}

我知道问题是我在UI中进行多线程但有人可以告诉我如何删除循环线程并用我的Asynctask的onPostExecute()方法替换它? (最好等待asynctask完成而不是等待......)

或者可以帮助我用多线程来纠正这个错误..

编辑:

我的asynctask:

public class DownloadAnnouncers extends AsyncTask<Void, Integer, Boolean>
    {
        public static Boolean loadFinished = false;

        //JSON variables..


        private static String url = null;

        Context context;

        public DownloadAnnouncers(Context context) 
        {
            this.context = context;
        }

        @Override
        protected void onPreExecute() 
        {
            super.onPreExecute();
        }

        protected Boolean doInBackground(Void... params) 
        {

        // fine retrieving all my JSON data in a global array..

        }

        @Override
        protected void onPostExecute(Boolean downloadedArray) 
        {
            super.onPostExecute(downloadedArray);
        Intent intent_to_tabhost = new Intent(context, MyTabActivity.class);
        intent_to_tabhost.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        context.startActivity(intent_to_tabhost);
        }
    }

但现在如果我按下取消按钮,我将返回到我的启动画面活动..而且我无法在onPostExecute方法中执行ctx.finish()..

2 个答案:

答案 0 :(得分:1)

您无法在未调用Handler的线程内创建Looper.prepare(),就像您的错误消息所示:

  

无法在未调用Looper.prepare()

的线程内创建处理程序

处理程序是在AsyncTask内创建的,所以这不是你最初可能会看到的。

如果您希望保持代码不变,则可以通过将AsyncTask的创建移动到自定义线程之外来修复它。

但是,您无需创建线程,您可以在ui线程中创建AsyncTask(它将在其自己的线程上运行,因此在工作时不会锁定您的手机),并执行任何操作在onPostExecute - 方法完成任务后,您需要。

class ExampleAsync extends AsyncTask<Void, Integer, Boolean>
{
    public static Boolean loadFinished = false;
    //JSON variables..
    private static String url = null;
    Context context;

    public DownloadAnnouncers(Context context){
        this.context = context;
    }

    protected Boolean doInBackground(Void... params){
        // fine retrieving all my JSON data in a global array..
    }

    @Override
    protected void onPostExecute(Boolean downloadedArray){
        Intent intent_to_tabhost = new Intent(context, MyTabActivity.class);
        startActivity(intent_to_tabhost);
    }
}

在你的活动中:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.splashscreen_second);
    AsyncTask myTask = new ExampleAsync(this);
    myTask.execute();
}

答案 1 :(得分:0)

不要等待asyncTask,而是使用onPostExecute,它只在任务完成后在UI线程上调用。

如果活动被销毁,您也应该取消该任务(例如,如果用户在显示启动画面时按下了后退按钮)。