Python Kivy启动器:应用程序崩溃,名称错误:全局名称';自动类';没有定义

Python Kivy启动器:应用程序崩溃,名称错误:全局名称';自动类';没有定义,python,android,notifications,kivy,Python,Android,Notifications,Kivy,我的kivy simple notifications应用程序在android上崩溃,在windows上运行良好 我正在尝试使用kivy、plyer和oscpy模块制作一个简单的应用程序,以检查android上服务的使用情况。 以下是该应用程序的功能: 它有一个按钮“set”,要求服务弹出下一分钟的通知 和另一个按钮“停止”停止服务 代码如下: main.py: from kivy.app import App from kivy.uix.textinput import TextInput f

我的kivy simple notifications应用程序在android上崩溃,在windows上运行良好

我正在尝试使用kivy、plyer和oscpy模块制作一个简单的应用程序,以检查android上服务的使用情况。 以下是该应用程序的功能: 它有一个按钮“set”,要求服务弹出下一分钟的通知 和另一个按钮“停止”停止服务

代码如下:

main.py:

from kivy.app import App
from kivy.uix.textinput import TextInput
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
import datetime
from kivy.clock import Clock
from kivy.utils import platform
from oscpy.client import OSCClient
from oscpy.server import OSCThreadServer
from plyer import notification

SERVICE_NAME = u'{packagename}.Service{servicename}'.format(
            packagename=u'org.kivy.oscservice',
            servicename=u'Pong'
)

class MyLayout(FloatLayout):
    def __init__(self,**kwargs):
        super(MyLayout,self).__init__(**kwargs)
        self.setB=Button(text="set",size_hint=(.25,.2),pos_hint={"x":.3,"y":.4})
        self.add_widget(self.setB)
        self.setB.bind(on_press=self.set)
        self.lis=[]

        self.server=None
        self.server=server=OSCThreadServer()
        server.listen(
            address=b'localhost',
            port=30002,
            default=True
        )
        server.bind(b'/message',self.passnow)
        server.bind(b'/date',self.passnow)
        self.client=OSCClient(b'localhost',3000)
        self.start_service()
        self.stop=Button(text="Stop",size_hint=(.25,.2),pos_hint={"x":.3,"y":.1})
        self.add_widget(self.stop)
        self.stop.bind(on_press=self.stop_service)

    def start_service(self):
        if platform=='android':
            service=autoclass(SERVICE_NAME)
            self.mActivity=autoclass(u'org.kivy.android.PythonActivity').mActivity
            argument=''
            service.start(self.mActivity,argument)
            self.service=service

        elif platform in ('linux','linux2','macos','win'):
            from runpy import run_path
            from threading import Thread
            self.service=Thread(
                target=run_path,
                args=['service.py'],
                kwargs={'run_name':'__main__'},
                daemon=True
            )
            self.service.start()
        else:
            raise NotImplementedError(
                "service start not implemented on this platform"
            )

    def stop_service(self):
        if self.service:
            if platform=="android":
                self.service.stop(self.mActivity)
            elif platform in ('linux','linux2','macos','win'):
                self.service.stop()
            else:
                raise NotImplementedError(
                    "service start not implemented on this platform"
                )
            self.service=None

    def set(self,button):
        time=datetime.datetime.now().strftime("%H:%M:%S")
        if int(time.split(':')[1])+1 not in self.lis:
            self.lis.append(int(time.split(':')[1])+1)
        q=[]
        for p in range(len(self.lis)):
            if self.lis[p]==60:
                self.lis[p]=0
            if self.lis[p]<=int(time.split(':')[1]) and self.lis[p]!=0:
                q.append(p)
        for p in q:
            self.lis.remove(self.lis[p])
    #    print(self.lis)
        self.client.send_message(b'/ping',list(self.lis),)

    def passnow(self,message):
        pass

class PlyerApp(App):
    def build(self):
        return MyLayout()

if __name__=="__main__":
    now=PlyerApp()
    now.run()
这在windows上运行良好 但当我尝试使用Kivy Launcher在android上运行它时,它在加载后崩溃,出现错误:

NameError:未定义全局名称“自动类”

autoclass()用于main.py中的以下行:

    def start_service(self):
        if platform=='android':
>            service=autoclass(SERVICE_NAME)
有人能告诉我为什么会发生错误以及如何解决它吗

这在windows上运行很好,但当我尝试在android上运行它时

只有在windows上不运行这一行的情况下,它才“起作用”


问题是您未能导入autoclass,无法从
jnius
模块导入它。

我避免了从jnius模块导入它,因为我无法在系统上安装pyjnius。但是经过一些搜索,我明白了我不需要显式地安装jnius。所以我在代码中使用autoclass之前添加了“fromjnius导入autoclass”。它成功了。谢谢
    def start_service(self):
        if platform=='android':
>            service=autoclass(SERVICE_NAME)