. lang。RuntimeException:不能在未调用Looper.prepare()的线程中创建处理程序

时间:2021-05-04 20:45:12

From a Service I am trying to start another IntentService but it is giving me an error when trying to pass in a new ResultReceiver with a new Handler as the parameter.

在一个服务中,我正在尝试启动另一个IntentService,但是当试图以新的处理程序作为参数传入一个新的ResultReceiver时,它会给我一个错误。

Can't figure out why it is giving me this error. Please help!

我不明白为什么它会给我这个错误。请帮助!

Line 797 in SocketIOService which the error has occurred is:

发生错误的SocketIOService中第797行是:

downloadIntent.putExtra(DownloadService.KEY_RECEIVER, new DownloadReceiver(new Handler()));

StackTrace Error

加误差

06-05 19:51:10.417: E/AndroidRuntime(7716): FATAL EXCEPTION: Thread-6358
06-05 19:51:10.417: E/AndroidRuntime(7716): java.lang.RuntimeException: Can't create handler inside thread that has not called Looper.prepare()
06-05 19:51:10.417: E/AndroidRuntime(7716):     at android.os.Handler.<init>(Handler.java:121)
06-05 19:51:10.417: E/AndroidRuntime(7716):     at com.walintukai.lfdate.SocketIOService$9.run(SocketIOService.java:797)
06-05 19:51:10.417: E/AndroidRuntime(7716):     at java.lang.Thread.run(Thread.java:856)

Code starting IntentService

代码开始IntentService

new Thread(new Runnable() {
        public void run() {
            try {
                JSONArray array = json.getJSONArray(0);

                if (array.length() > 0) {
                    ArrayList<String> coverPhotos = new ArrayList<String>();

                    for (int i = 0; i < array.length(); i++) {
                        String id = array.getJSONObject(i).getString(KEY_ID);
                        String name = array.getJSONObject(i).getString(KEY_NAME);
                        String genre = array.getJSONObject(i).getString(KEY_GENRE);

                        String platform = "";
                        JSONArray platformArray = array.getJSONObject(i).getJSONArray(KEY_PLATFORM);
                        for (int p = 0; p < platformArray.length(); p++) {
                            if (p == 0) { platform = platformArray.getString(p); }
                            else { platform = platform + ", " + platformArray.getString(p); }
                        }

                        String coverPhoto = "";
                        JSONArray coverPhotoArray = array.getJSONObject(i).getJSONArray(KEY_COVER_PHOTO);
                        for (int c = 0; c < coverPhotoArray.length(); c++) {
                            if (c == 0) { coverPhoto = coverPhotoArray.getString(c); }
                            else { coverPhoto = coverPhoto + ", " + coverPhotoArray.getString(c); }
                        }

                        Game game = new Game(id, name, genre, platform, coverPhoto);
                        if (!db.doesGameExist(id)) { db.createGame(game); }
                        else { db.updateGame(game); }

                        coverPhotos.add(coverPhoto);

                    }

                    Intent downloadIntent = new Intent(SocketIOService.this, DownloadService.class);
                    downloadIntent.putStringArrayListExtra(DownloadService.KEY_COVER_PHOTOS, coverPhotos);
                    downloadIntent.putExtra(DownloadService.KEY_RECEIVER, new DownloadReceiver(new Handler()));
                    startService(downloadIntent);
                }
            } 
            catch (JSONException e) { e.printStackTrace(); }
        }
    }).start();

ResultReceiver Code

ResultReceiver代码

private class DownloadReceiver extends ResultReceiver {

    public DownloadReceiver(Handler handler) {
        super(handler);
    }

    @Override
    protected void onReceiveResult(int resultCode, Bundle resultData) {
        super.onReceiveResult(resultCode, resultData);

        if (resultCode == DownloadService.RESULT_FINISHED) {
            Log.i("GAME COVER DOWNLOADS", "COMPLETE");
            if (mOnServiceListener != null) {
                mOnServiceListener.onGameListUpdated();
            }
        }
    }
}

IntentService Code

IntentService代码

public class DownloadService extends IntentService {

public static final String KEY_COVER_PHOTOS = "coverPhotos";
public static final String KEY_RECEIVER = "receiver";
public static final int RESULT_FINISHED = 1000;
private static final String URL_GAME_COVER = "https://test.com/images/game_cover/";

public DownloadService(String name) {
    super(name);
}

@Override
protected void onHandleIntent(Intent intent) {
    ArrayList<String> coverPhotos = intent.getStringArrayListExtra(KEY_COVER_PHOTOS);
    ResultReceiver receiver = (ResultReceiver) intent.getParcelableExtra(KEY_RECEIVER);

    for (String coverPhoto : coverPhotos) {
        // TODO: implement code to handle multiple photos

        try {
            URL url = new URL(URL_GAME_COVER + coverPhoto);
            URLConnection connection = url.openConnection();
            connection.connect();

            // This will be useful so that you can show a typical 0-100% progress bar
            int fileLength = connection.getContentLength();

            // Download the file
            InputStream input = new BufferedInputStream(url.openStream());
            String filepath = this.getExternalFilesDir(Environment.DIRECTORY_PICTURES).toString() + "/" + coverPhoto;
            OutputStream output = new FileOutputStream(filepath);

            byte data[] = new byte[1024];
            long total = 0;
            int count;
            while ((count = input.read(data)) != -1) {
                total += count;
                output.write(data, 0, count);
            }

            output.flush();
            output.close();
            input.close();
        } 
        catch (IOException e) { e.printStackTrace(); }

    }

    receiver.send(RESULT_FINISHED, null);

}

}

}

1 个解决方案

#1


2  

What type is your SocketIOService ? Is it really a Service or not?

你们的服务是什么类型的?这真的是一种服务吗?

If you create a Handler in a common thread which is not an Activity or Service,you should call Looper.prepare();first ,then a message bus is started in the thread and the handler works well.Or you will got the exception because general thread do not have the message bus(just a infinite loop in which the messages are consumed).

如果您在公共线程中创建的处理程序不是活动或服务,您应该调用Looper.prepare();首先,在线程中启动消息总线,处理程序运行良好。或者您会遇到异常,因为通用线程没有消息总线(只是使用消息的无限循环)。

EDIT: Oh,your handler is not in your Service,but in the sub-thread(in your new Thread(xxx).start),so this is a general thread which do not have a message bus.I think you should initalize your handler outside and then use it which is a handler of a Service.

编辑:哦,您的处理程序不在您的服务中,而是在子线程中(在您的新线程中(xxx .start)),因此这是一个没有消息总线的通用线程。我认为你应该将你的处理器嵌入到外部,然后使用它,它是一个服务的处理器。

#1


2  

What type is your SocketIOService ? Is it really a Service or not?

你们的服务是什么类型的?这真的是一种服务吗?

If you create a Handler in a common thread which is not an Activity or Service,you should call Looper.prepare();first ,then a message bus is started in the thread and the handler works well.Or you will got the exception because general thread do not have the message bus(just a infinite loop in which the messages are consumed).

如果您在公共线程中创建的处理程序不是活动或服务,您应该调用Looper.prepare();首先,在线程中启动消息总线,处理程序运行良好。或者您会遇到异常,因为通用线程没有消息总线(只是使用消息的无限循环)。

EDIT: Oh,your handler is not in your Service,but in the sub-thread(in your new Thread(xxx).start),so this is a general thread which do not have a message bus.I think you should initalize your handler outside and then use it which is a handler of a Service.

编辑:哦,您的处理程序不在您的服务中,而是在子线程中(在您的新线程中(xxx .start)),因此这是一个没有消息总线的通用线程。我认为你应该将你的处理器嵌入到外部,然后使用它,它是一个服务的处理器。