問題描述
這是我的錯誤代碼我認為這個錯誤有什么難的地方,我在論壇上找過,但沒有找到.
So here is my error code I think there's something hard in this error, i looked on forums but couldn't find out.
而且...我的手機出現(xiàn)錯誤(使用 kivy啟動器 python 3 以及當(dāng)我使用 buildozer 構(gòu)建時)但不在我的計算機上(ubuntu 18.0.4 和 windows 10)據(jù)我了解,該錯誤來自刪除引用的垃圾收集器,并且代碼嘗試在垃圾收集器之后訪問引用.但我不確定我是否理解垃圾收集器的事情
And ... i have the error on my phone (with kivy launcher python 3 and when i build with buildozer) but not on my computer (ubuntu 18.0.4 and windows 10) The error, from what i understand, comes from the garbage collector that delete a reference and the code try to access the reference after the garbage collector. but i am not sure if I rly understand the garbage collector thing
我嘗試了什么:
- 制作強引用",以便 gc 不會將其刪除:
id: id.__self__
在我的 kv 文件中
- 使用以下方法進行強引用":
self.refs = [
self.id.__self__,
self.id.__self__]
-使用ErrorHandler來處理錯誤,但錯誤總是會出現(xiàn)
-Use the ErrorHandler to handle the error but the error keep coming for ever
我認為是什么導(dǎo)致了錯誤,但我不知道如何解決它:
我用來向服務(wù)器發(fā)送請求的時鐘,但我不知道為什么(self.requestClient 是一個發(fā)送請求的函數(shù)):
the clock I use to send request to the server but i don't know why (self.requestClient is a function to send a request) :
C = Clock.schedule_interval(self.requestClient, 5)
C = Clock.schedule_interval(self.requestClient, 5)
此信息在 kivy 時鐘文檔 :
重要
回調(diào)是弱引用的:你負責(zé)保持一個引用您的原始對象/回調(diào).如果你不保留一個參考,ClockBase 永遠不會執(zhí)行你的回調(diào)
The callback is weak-referenced: you are responsible for keeping a reference to your original object/callback. If you don’t keep a reference, the ClockBase will never execute your callback
錯誤:
[ERROR ] Exception catched by ExceptionHandler
05-07 11:27:45.694 2788 2823 I python : Traceback (most recent call last):
05-07 11:27:45.694 2788 2823 I python : File path/kivy-launcher/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/launcher/kivy/core/window/window_sdl2.py", line 747, in mainloop
05-07 11:27:45.694 2788 2823 I python : File "/path/kivy-launcher/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/launcher/kivy/core/window/window_sdl2.py", line 479, in _mainloop
05-07 11:27:45.694 2788 2823 I python : File "/path/kivy-launcher/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/launcher/kivy/base.py", line 339, in idle
05-07 11:27:45.694 2788 2823 I python : File "/path/kivy-launcher/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/launcher/kivy/clock.py", line 591, in tick
05-07 11:27:45.694 2788 2823 I python : File "kivy/_clock.pyx", line 384, in kivy._clock.CyClockBase._process_events
05-07 11:27:45.694 2788 2823 I python : File "kivy/_clock.pyx", line 414, in kivy._clock.CyClockBase._process_events
05-07 11:27:45.694 2788 2823 I python : File "kivy/_clock.pyx", line 412, in kivy._clock.CyClockBase._process_events
05-07 11:27:45.694 2788 2823 I python : File "kivy/_clock.pyx", line 154, in kivy._clock.ClockEvent.tick
05-07 11:27:45.694 2788 2823 I python : File "kivy/_clock.pyx", line 86, in kivy._clock.ClockEvent.get_callback
05-07 11:27:45.694 2788 2823 I python : File "/path/kivy-launcher/.buildozer/android/platform/build-armeabi-v7a/build/python-installs/launcher/kivy/weakmethod.py", line 56, in is_dead
05-07 11:27:45.694 2788 2823 I python : ReferenceError: weakly-referenced object no longer exists
感謝閱讀!
推薦答案
官方文檔(Kv 語言編程指南) 說要在 KV 代碼中添加諸如 id_name: id_name.__self__
之類的強"引用,但不清楚這在哪里是必要的.更重要的是,它并沒有為我解決 ReferenceError:weakly-referenced object no longer exists
錯誤.
The official documentation (Kv language Programming Guide) says to add 'strong' references such as id_name: id_name.__self__
in the KV code, but it is unclear where this is necessary. What's more, it did not solve the ReferenceError: weakly-referenced object no longer exists
errors for me.
所做 的工作是通過將其添加到 buildozer.spec 文件的 requirements
行來強制 Buildozer 使用特定版本的 hostpython3
:
What did work is forcing Buildozer to use a specific version of hostpython3
by adding this to the requirements
line of the buildozer.spec file:
python3==3.7.5, hostpython3==3.7.5
還有一點需要注意:在將上述內(nèi)容添加到 requirements
之后,我返回并刪除了所有 __self__
引用,它仍然可以正常工作,因此顯然不再需要這些引用在 Kivy KV 語言中.
One more note: after adding the above to requirements
, I went back and removed all my __self__
references and it still worked fine, so apparently these are no longer needed in Kivy KV language.
這要歸功于 leo10011 的精彩回答.
2020-05-19 更新:這個錯誤 據(jù)報道已在 Kivy 2.0 中修復(fù).
這篇關(guān)于Kivy:錯誤弱引用對象(在時鐘函數(shù)中)不再存在的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!