From 9da4c93582f28352a8f11acbb917d5ce9c07183e Mon Sep 17 00:00:00 2001 From: olbb Date: Tue, 19 Feb 2019 10:33:59 +0800 Subject: [PATCH] Call Looper.prepareMainLooper() to avoid exception Some devices internally create a Handler when creating an input Surface, causing an exception: > Surface: java.lang.RuntimeException: Can't create handler inside > thread that has not called Looper.prepare() As a workaround, call Looper.prepareMainLooper() beforehand. Fixes: - - Signed-off-by: Romain Vimont --- .../src/main/java/com/genymobile/scrcpy/ScreenEncoder.java | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java index 8357b06150..273422a69d 100644 --- a/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java +++ b/server/src/main/java/com/genymobile/scrcpy/ScreenEncoder.java @@ -7,6 +7,7 @@ import android.media.MediaCodecInfo; import android.media.MediaFormat; import android.os.IBinder; +import android.os.Looper; import android.view.Surface; import java.io.FileDescriptor; @@ -54,6 +55,11 @@ public boolean consumeRotationChange() { } public void streamScreen(Device device, FileDescriptor fd) throws IOException { + // Some devices internally create a Handler when creating an input Surface, causing an exception: + // "Can't create handler inside thread that has not called Looper.prepare()" + // + Looper.prepareMainLooper(); + MediaFormat format = createFormat(bitRate, frameRate, iFrameInterval); device.setRotationListener(this); boolean alive;