過去 3 時間、解決策を見つけるのに苦労しました。他の回答では、その理由はコードが別のスレッドで実行されているためだと述べていますが、これは理にかなっていますが、Java を知らない人として、pyjnius
.ここで「pyjnius」へ: Android 「ビュー階層を作成した元のスレッドのみがそのビューにアクセスできます。」
from kivy.app import App
from kivy.uix.relativelayout import RelativeLayout
from jnius import autoclass
CurrentActivity = autoclass('org.kivy.android.PythonActivity').mActivity
view = CurrentActivity.getWindow().getDecorView()
view.setKeepScreenOn(True)
class Root(RelativeLayout):
def __init__(self):
super().__init__()
class SomeApp(App):
def build(self):
return Root()
SomeApp().run()
android.view.Window の別のメソッドを使用すると、同じエラーが発生します。
CurrentActivity = autoclass('org.kivy.android.PythonActivity').mActivity
WindowManager = autoclass('android.view.WindowManager$LayoutParams')
CurrentActivity.getWindow().addFlags(WindowManager.FLAG_KEEP_SCREEN_ON)
の出力adb logcat python
:
12-15 16:48:21.692 18623 18646 I python : Traceback (most recent call last):
12-15 16:48:21.692 18623 18646 I python : WARNING:kivy:stderr: Traceback (most recent call last):
12-15 16:48:21.693 18623 18646 I python : File "main.py", line 20, in <module>
12-15 16:48:21.693 18623 18646 I python : WARNING:kivy:stderr: File "main.py", line 20, in <module>
12-15 16:48:21.694 18623 18646 I python : view.setKeepScreenOn(True)
12-15 16:48:21.694 18623 18646 I python : WARNING:kivy:stderr: view.setKeepScreenOn(True)
12-15 16:48:21.695 18623 18646 I python : File "jnius/jnius_export_class.pxi", line 734, in jnius.JavaMethod.__call__ (jnius/jnius.c:25894)
12-15 16:48:21.695 18623 18646 I python : WARNING:kivy:stderr: File "jnius/jnius_export_class.pxi", line 734, in jnius.JavaMethod.__call__ (jnius/jnius.c:25894)
12-15 16:48:21.697 18623 18646 I python : File "jnius/jnius_export_class.pxi", line 828, in jnius.JavaMethod.call_method (jnius/jnius.c:27082)
12-15 16:48:21.697 18623 18646 I python : WARNING:kivy:stderr: File "jnius/jnius_export_class.pxi", line 828, in jnius.JavaMethod.call_method (jnius/jnius.c:27082)
12-15 16:48:21.698 18623 18646 I python : File "jnius/jnius_utils.pxi", line 93, in jnius.check_exception (jnius/jnius.c:4012)
12-15 16:48:21.698 18623 18646 I python : WARNING:kivy:stderr: File "jnius/jnius_utils.pxi", line 93, in jnius.check_exception (jnius/jnius.c:4012)
12-15 16:48:21.699 18623 18646 I python : jnius.JavaException: JVM exception occurred: Only the original thread that created a view hierarchy can touch its views.
12-15 16:48:21.699 18623 18646 I python : WARNING:kivy:stderr: jnius.JavaException: JVM exception occurred: Only the original thread that created a view hierarchy can touch its views.
12-15 16:48:21.822 18623 18646 I python : Python for android ended.
12-15 16:48:21.830 18623 18623 V SDL : onWindowFocusChanged(): false
12-15 16:48:21.833 18623 18623 V PythonActivity: onPause()
12-15 16:48:21.833 18623 18623 V SDL : onPause()
12-15 16:48:21.833 18623 18623 V SDL : nativePause()
12-15 16:48:21.833 18623 18623 F libc : Fatal signal 11 (SIGSEGV), code 1, fault addr 0x0 in tid 18623 (ample.myaasdqpp)