简体   繁体   English

使用AsyncTask时发生ExceptionInInitializerError

[英]ExceptionInInitializerError when using AsyncTask

I am using an AsyncTask to read data from a file. 我正在使用AsyncTask从文件读取数据。 I get the aforementioned error when I run the application. 运行应用程序时出现上述错误。

The error messages are: 错误消息是:

03-29 20:06:08.445: E/AndroidRuntime(13191): java.lang.ExceptionInInitializerError 03-29 20:06:08.445: E/AndroidRuntime(13191): at com.google.app.BouncingBall.HighScore.loadFromFile(HighScore.java:81) 03-29 20:06:08.445: E/AndroidRuntime(13191): at com.google.app.BouncingBall.HighScore.(HighScore.java:24) 03-29 20:06:08.445: E/AndroidRuntime(13191): at com.google.app.BouncingBall.BouncingBallActivity$BouncingBallView.init(BouncingBallActivity.java:185) 03-29 20:06:08.445: E/AndroidRuntime(13191): at com.google.app.BouncingBall.BouncingBallActivity$BouncingBallView.run(BouncingBallActivity.java:173) 03-29 20:06:08.445: E/AndroidRuntime(13191): at java.lang.Thread.run(Thread.java:1019) 03-29 20:06:08.445: E/AndroidRuntime(13191): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 03-29 20:06:08.445: E/AndroidRuntime(13191): at android.os.Handler.(Handler.java:121) 03-29 20:06:08.445:E / AndroidRuntime(13191):java.lang.ExceptionInInitializerError 03-29 20:06:08.445:E / AndroidRuntime(13191):位于com.google.app.BouncingBall.HighScore.loadFromFile( HighScore.java:81)03-29 20:06:08.445:E / AndroidRuntime(13191):at com.google.app.BouncingBall.HighScore。(HighScore.java:24)03-29 20:06:08.445:E / AndroidRuntime(13191):位于com.google.app.BouncingBall.BouncingBallActivity $ BouncingBallView.init(BouncingBallActivity.java:185)03-29 20:06:08.445:E / AndroidRuntime(13191):位于com.google.app。 BouncingBall.BouncingBallActivity $ BouncingBallView.run(BouncingBallActivity.java:173)03-29 20:06:08.445:E / AndroidRuntime(13191):at java.lang.Thread.run(Thread.java:1019)03-29 20: 06:08.445:E / AndroidRuntime(13191):由以下原因引起:java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序03-29 20:06:08.445:E / AndroidRuntime(13191) ):位于android.os.Handler。(Handler.java:121)

The Code 编码

private void loadFromFile()
    {
        new AsyncDataStorage().execute(FILENAME);
    }


class AsyncDataStorage extends AsyncTask<String, Integer, Boolean> {

        protected Boolean doInBackground(String... args) {
            try {
                FileInputStream fis = context.openFileInput(FILENAME);
                byte[]  raw = new byte[fis.available()];
                String rawData=null;
                while(fis.read()!=-1)
                {
                    rawData = new String(raw);
                }
                return (processRawData(rawData));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
                return false;
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                return false;
            }

        }
03-29 20:06:08.445: E/AndroidRuntime(13191): Caused by: java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare() 03-29 20:06:08.445: 

Quoting the documentation for AsyncTask : 引用AsyncTask的文档

The task instance must be created on the UI thread. 必须在UI线程上创建任务实例。

In your cask, the task instance is not being created on the main application (aka, UI) thread, which is what leads to this exception. 在您的酒桶中,没有在主应用程序(aka,UI)线程上创建任务实例,这就是导致此异常的原因。

只需将对com.google.app.BouncingBall.HighScore.loadFromFile每次调用或在其中创建的AsyncTask封装在Runnable中,然后将其发布到绑定到UI线程的Handler中即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM