我是工程学士学位的学生,仍在大二,但我正在尝试使用 Python 和 Kivy(我尝试自学)开发一个基本的助手应用程序。
我为应用程序编写的代码在我的 Windows 系统上运行得很好,但是为了将源代码打包到 apk 中,我使用了 Google Collab 帐户并安装了 Ubuntu 和 buildozer 环境。 apk 也已成功生成,但在任何 Android 设备上安装后,应用程序在显示“(Kivy Logo)Loading...”后立即崩溃
我尝试更改部分代码以及规范文件内容,但没有任何帮助。 我是 Kivy 和应用程序开发的新手,所以我(老实说)对 logcat 和调试 apk 不太了解。
如果有人可以帮助我,我将非常感激。
这是我的代码:
from kivy.app import App
from kivy.config import Config
Config.set('graphics','resizable',True)
from kivy.uix.button import Button
from kivy.uix.floatlayout import FloatLayout
from kivy.uix.pagelayout import PageLayout
from kivy.uix.gridlayout import GridLayout
import datetime
from kivy.uix.popup import Popup
from kivy.uix.label import Label
from kivy.uix.textinput import TextInput
import os
from kivy.clock import Clock
from kivy.core.window import Window
OldNameOfFile=""
OldText=""
firsttime=1
pcheck=0
FileListString=""
class MyLayout(FloatLayout):
def ButtonMaker(self):
self.Timeb=Button(
text="Time",size_hint=(.3,.3),pos_hint={"x":.09,"y":.6})
self.WriteB=Button(
text="Press here to \nwrite a note",size_hint=(.3,.3),pos_hint={"x":.6,"y":.6})
self.NoteB=Button(
text="Press here to view\n a written note",size_hint=(.3,.3),pos_hint={"x":.09,"y":.2})
self.SWB=Button(
text="Press here for \nStop-Watch",size_hint=(.3,.3),pos_hint={"x":.6,"y":.2})
self.STOPWATCHStartB=Button(
text="Start",size_hint=(.25,.20),pos_hint={"x":.04,"y":.3})
self.STOPWATCHStopB=Button(
text="Stop",size_hint=(.25,.20),pos_hint={"x":.36,"y":.3})
self.STOPWATCHCloseB=Button(
text="Close",size_hint=(.25,.20),pos_hint={"x":.68,"y":.3})
self.STOPWATCHPauseB=Button(
text="Pause",size_hint=(.25,.20),pos_hint={"x":.2,"y":.05})
self.STOPWATCHResetB=Button(
text="Reset",size_hint=(.25,.20),pos_hint={"x":.53,"y":.05})
self.STOPWATCHDisp=TextInput(
text=" 00:00:00:00",size_hint=(.9,.3),font_size='125sp',readonly=True,pos_hint={"x":.05,"y":.6})
self.TimeBTime=Button(
text="The time is :",background_color="black",font_size=40,pos_hint={"x":0,"y":.3})
self.TimeBCloseB=Button(
text="Click to close",size_hint=(.2,.2),pos_hint={"x":.35,"y":.2})
self.WriteBNameB=Button(
text="Enter name of file here : (or Enter name of already existing file to edit before entering text)",size_hint=(1,.09),background_color="black",pos_hint={"x":0.01,"y":.9})
self.WriteBTIB=Button(
text="Enter text here :",size_hint=(1,.09),background_color="black",pos_hint={"x":0.01,"y":.78})
self.WriteBSubmitB=Button(
text="Save",size_hint=(.4,.1),pos_hint={"x":0.07,"y":.1})
self.WriteBCloseB=Button(
text="Close",size_hint=(.4,.1),pos_hint={"x":.53,"y":.1})
self.WriteBTI=TextInput(
font_size=30,size_hint=(.95,.55),pos_hint={"x":0.02,"y":.25},multiline=True)
self.WriteBName=TextInput(
font_size=20,size_hint=(.95,.07),pos_hint={"x":0.02,"y":.85})
self.WriteBp=Button(
text="Empty name error...",background_color="black",size_hint=(.4,.08),pos_hint={"x":0.3,"y":0.01})
self.NoteBName=TextInput(
text="Select a file name from below files",font_size=20,size_hint=(.95,.07),pos_hint={"x":.02,"y":.85})
self.NoteBNameB=Button(
text="Enter name of file here :",size_hint=(1,.09),background_color="black",pos_hint={"x":0.01,"y":.9})
self.NoteBTextData=TextInput(
font_size=20,size_hint=(.95,.55),pos_hint={"x":0.02,"y":.25},readonly=True,multiline=True)
self.NoteBTextDataB=Button(
text="Data of files :",size_hint=(1,.09),background_color="black",pos_hint={"x":0.01,"y":.78})
self.NoteBSubmitB=Button(
text="Submit",size_hint=(.4,.1),pos_hint={"x":0.07,"y":.13})
self.NoteBCloseB=Button(
text="Close",size_hint=(.4,.1),pos_hint={"x":0.53,"y":.13})
self.NoteBDeleteB=Button(
text="Delete this",size_hint=(.38,.09),pos_hint={"x":0.3,"y":0.02})
self.NoteBp=Button(
text="Empty name error...",background_color="black",size_hint=(.4,.1),pos_hint={"x":0.3,"y":0.02})
self.HomePage1List=[
self.Timeb,self.WriteB,self.NoteB,self.SWB]
self.STOPWATCHList=[
self.STOPWATCHStartB,self.STOPWATCHStopB,self.STOPWATCHCloseB,self.STOPWATCHPauseB,self.STOPWATCHResetB,self.STOPWATCHDisp]
self.TimeBList=[
self.TimeBTime,self.TimeBCloseB]
self.WriteBList=[
self.WriteBNameB,self.WriteBTIB,self.WriteBName,self.WriteBTI,self.WriteBSubmitB,self.WriteBCloseB]
self.NoteBList=[
self.NoteBNameB,self.NoteBName,self.NoteBTextDataB,self.NoteBTextData,self.NoteBSubmitB,self.NoteBCloseB,self.NoteBDeleteB]
def BindButtons(self):
self.SWB.bind(on_press=self.StopWatchPress)
self.Timeb.bind(on_press=self.TimeBPress)
self.WriteB.bind(on_press=self.WriteBPress)
self.NoteB.bind(on_press=self.NoteBPress)
self.STOPWATCHStartB.bind(on_press=self.OnStart)
self.STOPWATCHStopB.bind(on_press=self.OnStop)
self.STOPWATCHResetB.bind(on_press=self.OnReset)
self.STOPWATCHPauseB.bind(on_press=self.OnPause)
self.STOPWATCHCloseB.bind(on_press=self.StopWatchClose)
self.TimeBCloseB.bind(on_press=self.TimeBClose)
self.WriteBCloseB.bind(on_press=self.WriteBClose)
self.WriteBSubmitB.bind(on_press=self.Saveandwrite)
self.NoteBSubmitB.bind(on_press=self.OpenFile)
self.NoteBCloseB.bind(on_press=self.NoteBClose)
self.NoteBDeleteB.bind(on_press=self.NoteBDelete)
def AddHomePage1(self):
for b in self.HomePage1List:
self.add_widget(b)
def RemHomePage1(self):
for b in self.HomePage1List:
self.remove_widget(b)
def __init__(self,**kwargs):
super().__init__(**kwargs)
self.ButtonMaker()
self.BindButtons()
self.AddHomePage1()
def StopWatchPress(self,button):
self.RemHomePage1()
self.STOPWATCHpause=0
self.STOPWATCHstop=0
Clock.unschedule(self.increment_STOPWATCHtime)
self.STOPWATCHtime=["00","00","00","0"]
self.STOPWATCHDisp.text=" 00:00:00:00"
for b in self.STOPWATCHList:
self.add_widget(b)
def TimeBPress(self,button):
self.RemHomePage1()
time=datetime.datetime.now()
time=time.strftime("%H:%M:%S")
self.TimeBTime.text=self.TimeBTime.text[:13]+str(time)
for b in self.TimeBList:
self.add_widget(b)
def WriteBPress(self,button):
self.RemHomePage1()
global firsttime
firsttime=1
for b in self.WriteBList:
self.add_widget(b)
def NoteBPress(self,button):
self.RemHomePage1()
path=os.getcwd()
FileList=os.listdir(path)
global FileListString
FileListString="Names of all files are : \n"
for k in FileList:
if k[-4:]==".txt":
FileListString=FileListString+k+"\n"
self.NoteBTextData.text=FileListString
for b in self.NoteBList:
self.add_widget(b)
def StopWatchClose(self,button):
for b in self.STOPWATCHList:
self.remove_widget(b)
self.AddHomePage1()
def TimeBClose(self,button):
self.remove_widget(self.TimeBCloseB)
self.remove_widget(self.TimeBTime)
self.AddHomePage1()
def Saveandwrite(self,button):
global OldNameOfFile
global OldText
global firsttime
if firsttime==1 or (OldNameOfFile!=self.WriteBName.text.strip() or OldText!=self.WriteBTI.text):
file="new"
NameOfFile=self.WriteBName.text.strip()
else:
file="old"
if file=="old":
return
global pcheck
OldNameOfFile=NameOfFile
if NameOfFile=="":
if pcheck==0:
self.add_widget(self.WriteBp)
pcheck=1
return
if NameOfFile!="":
self.remove_widget(self.WriteBp)
pcheck=0
if NameOfFile[-4:]==".txt":
NameOfFile=NameOfFile[:-4]
try:
NewF=open(NameOfFile+".txt","r")
if self.WriteBTI.text=="":
self.WriteBTI.text=NewF.read()
NewF.close()
except:
pass
NewF=open(NameOfFile+".txt","w")
self.WriteBName.text=NameOfFile
Text=self.WriteBTI.text
OldText=Text
NewF.write(Text)
NewF.close()
firsttime=0
return
def WriteBClose(self,button):
for b in self.WriteBList:
self.remove_widget(b)
global pcheck
global OldNameOfFile
global firsttime
firsttime=1
OldNameOfFile=""
self.WriteBName.text=""
self.WriteBTI.text=""
if pcheck==1:
pcheck=0
self.remove_widget(self.WriteBp)
self.AddHomePage1()
def OpenFile(self,button):
global FileListString
NameOfFile=self.NoteBName.text.strip()
global pcheck
if NameOfFile=="":
if pcheck==0:
self.add_widget(self.NoteBp)
pcheck=1
return
if NameOfFile!="":
self.remove_widget(self.NoteBp)
pcheck=0
try:
if NameOfFile[-4:]==".txt":
NameOfFile=NameOfFile[:-4]
NewF=open(NameOfFile+".txt","r")
self.NoteBTextData.text=NewF.read()
except FileNotFoundError:
self.NoteBTextData.text=FileListString
self.NoteBName.text="No Such File.. Select a name from below files.."
def NoteBDelete(self,button):
NameOfFile=self.NoteBName.text.strip()
if NameOfFile[-4:]==".txt":
NameOfFile=NameOfFile[:-4]
try:
os.remove(NameOfFile+".txt")
except:
pass
path=os.getcwd()
FileList=os.listdir(path)
global FileListString
FileListString="Names of all files are : \n"
for k in FileList:
if k[-4:]==".txt":
FileListString=FileListString+k+"\n"
self.NoteBTextData.text=FileListString
self.NoteBName.text="Select a file name from below files"
def NoteBClose(self,button):
for b in self.NoteBList:
self.remove_widget(b)
global pcheck
global FileListString
FileListString=""
self.NoteBName.text="Select a file name from below files"
if pcheck==1:
pcheck=0
self.remove_widget(self.NoteBp)
self.AddHomePage1()
def increment_STOPWATCHtime(self,interval):
if int(self.STOPWATCHtime[3])<75:
self.STOPWATCHtime[3]=str(int(self.STOPWATCHtime[3])+2)
else:
self.STOPWATCHtime[3]=str(int(self.STOPWATCHtime[3])+4)
if int(self.STOPWATCHtime[3])>90:
self.STOPWATCHtime[2]=str(int(self.STOPWATCHtime[2])+1)
self.STOPWATCHtime[3]="00"
if self.STOPWATCHtime[2]=="60":
self.STOPWATCHtime[1]=str(int(self.STOPWATCHtime[1])+1)
self.STOPWATCHtime[2]="00"
self.STOPWATCHtime[3]="30"
if self.STOPWATCHtime[1]=="60":
self.STOPWATCHtime[0]=str(int(self.STOPWATCHtime[0])+1)
self.STOPWATCHtime[1]="00"
if self.STOPWATCHtime[0]=="24":
self.STOPWATCHtime=["00","00","00","00"]
for k in range(4):
if int(self.STOPWATCHtime[k])<10:
self.STOPWATCHtime[k]="0"+str(int(self.STOPWATCHtime[k]))
self.STOPWATCHDisp.text=" "
for k in range(4):
self.STOPWATCHDisp.text+=self.STOPWATCHtime[k]
if k<3: self.STOPWATCHDisp.text+=":"
def OnPause(self,button):
self.STOPWATCHpause=1
Clock.unschedule(self.increment_STOPWATCHtime)
def OnReset(self,button):
Clock.unschedule(self.increment_STOPWATCHtime)
self.STOPWATCHtime=["00","00","00","00"]
self.STOPWATCHDisp.text=" 00:00:00:00"
self.STOPWATCHpause=0
def OnStart(self,button):
Clock.unschedule(self.increment_STOPWATCHtime)
if self.STOPWATCHpause==0 and self.STOPWATCHstop!=1:
self.STOPWATCHtime=["00","00","00","00"]
self.STOPWATCHDisp.text=" 00:00:00:00"
elif self.STOPWATCHstop==1:
self.STOPWATCHtime=["00","00","00","00"]
self.STOPWATCHDisp.text=" 00:00:00:00"
Clock.schedule_interval(self.increment_STOPWATCHtime,0.02)
self.STOPWATCHpause=0
self.STOPWATCHstop=0
def OnStop(self,button):
Clock.unschedule(self.increment_STOPWATCHtime)
self.STOPWATCHstop=1
self.STOPWATCHpause=0
class DemoApp(App):
def build(self):
return MyLayout()
if __name__=="__main__":
demo=DemoApp()
demo.run()
如果我没有错的话,我还必须添加操作系统模块的配方,但我也不知道其过程。任何帮助将非常感激。
谢谢你。
我找到了答案。
https://play.google.com/store/apps/details?id=com.apptiva.logviewer
使用上面的应用程序,我找到了应用程序崩溃时的日志并了解了发生的错误。 它是:按钮的背景颜色在 Windows 上可以是字符串,但需要 rgba 代码才能在 Android 上运行。也许Android还没有添加该功能,也可以接受字符串作为颜色。在上面的代码中,我将其指定为“黑色”,我必须将其更改为(0,0,0,1)。 基本上,这个错误是“无法将字符串转换为浮点:b”,这就是我理解问题出在“黑色”的方式。
特别赞扬 Kivy 启动器应用程序: https://play.google.com/store/apps/details?id=org.kivy.pygame 在检查错误是否已纠正方面提供了很大帮助,而无需实际打包应用程序,这需要花费大量时间。
我还更改了创建和保存文件的路径,然后再次读取它们。
我也遇到了完全相同的问题。我通过运行这个 adb 命令解决了这个问题并发现了我的错误。
adb -s a51e9533 logcat *:S python:D
-s
之后是我的adb设备a51e9533
。也许你的不一样。
您可以通过运行
adb devices
找到您的设备 ID