Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

' Only the original thread that created a view hierarchy can touch its views.' error when attempting to access android.view.Window methods #376

Closed
fr3nzy opened this issue Dec 15, 2018 · 1 comment
Labels
support upstream Topics related to changes/bugs/quirks in upstream PyJNIus uses. (Python, Java, specific JNI, ...)

Comments

@fr3nzy
Copy link

fr3nzy commented Dec 15, 2018

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()

The same error occurs when using a different method of android.view.Window:

CurrentActivity  = autoclass('org.kivy.android.PythonActivity').mActivity
WindowManager = autoclass('android.view.WindowManager$LayoutParams')
CurrentActivity.getWindow().addFlags(WindowManager.FLAG_KEEP_SCREEN_ON)
Kivy 1.10.1
Python 3.6

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)
@fr3nzy fr3nzy changed the title ' Only the original thread that created a view hierarchy can touch its views.' error when attempting to access View class ' Only the original thread that created a view hierarchy can touch its views.' error when attempting to access android.view.Window methods Dec 15, 2018
@KeyWeeUsr KeyWeeUsr added support upstream Topics related to changes/bugs/quirks in upstream PyJNIus uses. (Python, Java, specific JNI, ...) labels Dec 15, 2018
@KeyWeeUsr
Copy link
Contributor

The error basically means that you are running PyJNIus, and your application, in a non-main Thread which is the default behavior for android, I believe. Correct me if I'm wrong.

Imagine it like a Kivy main loop which polls for various events and what you try to do is like sleep(1). By default you'd freeze your UI in Kivy unless using kivy.clock.Clock, but what Android does is that it does not allow you to do anything like that. You'll need to explicitly run your code on the main Thread via java.lang.Runnable and android.app.Activity.runOnUiThread, e.g. like this example.

Or, you can just import the behavior from the android package in android.runnable.run_on_ui_thread, but don't forget to add it to your requirements too!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
support upstream Topics related to changes/bugs/quirks in upstream PyJNIus uses. (Python, Java, specific JNI, ...)
Projects
None yet
Development

No branches or pull requests

2 participants