当我按屏幕上的“继续”按钮或屏幕上的“到菜单”时 应用程序崩溃。它只发生在 Android 上,在 PC 上它工作得很好(如果 GPS 功能被禁用)。最后的屏幕只是显示需要什么并将写入 Json 文件。 我现在不明白为什么有些按钮确实会使应用程序崩溃,而其他按钮却不会,而它们都使用一种方法。
有什么方法可以在 Android 上查看错误代码来调试我的应用程序。
提前谢谢您
from kivy.lang import Builder
from plyer import gps
from kivy.app import App
from kivy.properties import StringProperty
from kivy.clock import mainthread
from kivy.utils import platform
from kivy.uix.screenmanager import ScreenManager, Screen, NoTransition
import json
import time
Builder.load_file(filename='main.kv')
class GpsMixin():
def start_gps(self) -> None:
gps.start(0,Trailz().MinDistanceSetting)
def stop_gps(self) -> None:
gps.stop()
def Pass(self) -> None:
pass
class GpsRecordingScreen(Screen,GpsMixin):
pass
class GpsPausedScreen(Screen,GpsMixin):
pass
class GpsInfoScreen(Screen,GpsMixin):
pass
class MenuScreen(Screen):
pass
class TestScreen(Screen):
pass
MainScreenManager = ScreenManager()
class Trailz(App):
gps_location = StringProperty()
def __init__(self, **kwargs) -> None:
super().__init__(**kwargs)
self._GPSJsonDict = {'GPSData':{}}
self._jsonTrailPath = "saved_trails/"
self._MinDistancePar = 1
def switch_toSTR(self, screenSTR : str) -> None:
MainScreenManager.switch_to(MainScreenManager.get_screen(screenSTR))
def request_android_permissions(self):
from android.permissions import request_permissions, Permission
def callback(permissions, results):
if all([res for res in results]):
print("callback. All permissions granted.")
else:
print("callback. Some permissions refused.")
request_permissions([Permission.ACCESS_COARSE_LOCATION,Permission.ACCESS_FINE_LOCATION], callback)
def build(self):
global MainScreenManager
MainScreenManager.add_widget(GpsInfoScreen(name='GpsInfoScreen'))
MainScreenManager.add_widget(MenuScreen(name='MenuScreen'))
MainScreenManager.add_widget(GpsRecordingScreen(name='GpsRecordingScreen'))
MainScreenManager.add_widget(GpsPausedScreen(name='GpsPausedScreen'))
MainScreenManager.add_widget(TestScreen(name='TestScreen'))
try:
gps.configure(on_location=self.on_location)
except NotImplementedError:
import traceback
traceback.print_exc()
if platform == "android":
print("gps.py: Android detected. Requesting permissions")
self.request_android_permissions()
return MainScreenManager
def open_gps_recording(self) -> None:
if platform == "ios" or platform == "android":
MainScreenManager.switch_to(MainScreenManager.get_screen('GpsRecordingScreen'))
else:
pass
@mainthread
def on_location(self, **kwargs) -> None:
self._GPSJsonDict['GPSData'][int(time.time()) % 2592000] = kwargs
self.gps_location = '\n'.join([
'{}={}'.format(k, v) for k, v in kwargs.items()])
def Save_GPS_to_json(self) -> None:
if MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text != "" and MainScreenManager.get_screen('GpsInfoScreen').ids.GPSDescriptionInput.text != "" and MainScreenManager.get_screen('GpsInfoScreen').ids.GPSMinDistanceInput.text != "":
self._GPSJsonDict['name'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text
self._GPSJsonDict['discription'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSDescriptionInput.text
self._GPSJsonDict['MinDistance'] = MainScreenManager.get_screen('GpsInfoScreen').ids.GPSMinDistanceInput.text
#with open(str(self._jsonTrailPath + "_".join(MainScreenManager.get_screen('GpsInfoScreen').ids.GPSNameInput.text.split(" ")) + ".json"), "w") as File:
#json.dump(self._GPSJsonDict, File)
MainScreenManager.get_screen('TestScreen').ids.TestLabel.text = str(self._GPSJsonDict)
MainScreenManager.switch_to(MainScreenManager.get_screen('TestScreen'))
@property
def MinDistanceSetting(self) -> int:
return self._MinDistancePar
@property
def GPSJsonDict(self) -> dict:
return self._GPSJsonDict
if __name__ == '__main__':
Trailz().run()
<MenuScreen>
BoxLayout:
Button:
text: "start trail recording"
on_press: app.open_gps_recording()
<GpsRecordingScreen>:
BoxLayout:
Label:
text: app.gps_location
ToggleButton:
id: StartRecording
text: "start" if self.state == 'normal' else "stop"
on_press:
root.stop_gps() if self.state == 'normal' else root.Pass()
app.switch_toSTR('GpsPausedScreen') if self.state == 'normal' else root.start_gps()
<GpsPausedScreen>
BoxLayout:
Button:
text: "continue"
on_press:
app.switch_toSTR('GpsRecordingScreen')
Button:
text: "stop"
on_press:
app.switch_toSTR('GpsInfoScreen')
<GpsInfoScreen>
GridLayout:
cols: 1
Image:
size_hint: 2, 2
source: 'map.jpeg'
size: self.texture_size
TextInput:
size_hint: 0.3, 0.3
id: GPSNameInput
hint_text: "name"
TextInput:
size_hint: 0.3, 0.3
id: GPSDescriptionInput
hint_text: "description"
TextInput:
size_hint: 0.3, 0.3
id: GPSMinDistanceInput
hint_text: "delta"
Button:
size_hint: 0.3, 0.3
text: "Сохранить"
on_press:app.Save_GPS_to_json()
<TestScreen>
GridLayout:
cols: 1
Label:
id: TestLabel
text: "None"
Button:
size_hint: 0.3, 0.3
text: "to menu"
on_press: app.switch_toSTR('MenuScreen')
[app]
title = Trailz
package.name = trailz
package.domain = org.gpstest
source.dir = .
source.include_exts = py,png,jpg,kv,atlas,jpeg
version = 0.1
requirements = python3, kivy, android, https://github.com/HyTurtle/plyer/archive/master.zip
orientation = portrait
osx.python_version = 3
osx.kivy_version = 1.9.1
fullscreen = 0
android.permissions = INTERNET,ACCESS_FINE_LOCATION,ACCESS_COARSE_LOCATION
android.archs = arm64-v8a, armeabi-v7a
android.allow_backup = True
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0
ios.codesign.allowed = false
[buildozer]
log_level = 2
warn_on_root = 1
我尝试过使用 'app.root.switch_to(app.root.get_screen('MenuScreen'))'
没有详细记录,但是当您使用
switch_to()
的 ScreenManager
方法时,它实际上会从 Screen
中删除当前的 ScreenManager
。在您的 switch_toSTR()
方法中,尝试更改:
MainScreenManager.switch_to(MainScreenManager.get_screen(screenSTR))
至:
MainScreenManager.current = screenSTR