java.lang.RuntimeException:无法在未调用Looper.prepare()的线程内创建处理程序

时间:2014-06-06 03:02:29

标签: android android-handler

Service我正在尝试启动另一个IntentService,但在尝试传入新的ResultReceiver并使用新的Handler作为参数。

无法弄清楚为什么它会给我这个错误。请帮忙!

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

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

StackTrace错误

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)

代码启动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代码

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代码

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 个答案:

答案 0 :(得分:2)

您的SocketIOService是什么类型的?它真的是Service吗?

如果您在不是HandlerActivity的公共线程中创建Service,则应首先调用Looper.prepare();,然后在线程和处理程序运行良好。或者你会得到异常,因为一般线程没有消息总线(只是一个消耗消息的无限循环)。

编辑:哦,您的处理程序不在您的服务中,而是在子线程中(在您的新线程(xxx).start中),所以这是一个没有的通用线程一个消息总线。我认为你应该把你的处理程序放在外面,然后使用它是Service的处理程序。