【问题标题】:APK Crashes immediately on android while clicking on the app icon, Kivymd单击应用程序图标 Kivymd 时,APK 在 Android 上立即崩溃
【发布时间】:2022-12-10 19:55:27
【问题描述】:

我正在使用 Kivy 框架制作一个 android 应用程序项目。我已经创建了 50% 的项目。它在 Windows 上完美运行然后我想首先尝试检查它是否可以轻松转换为 APK 文件并在 android 上运行?我创建了 4 个屏幕登入,注册,主屏幕和 >相机界面.

使用来自网络摄像头的 opencv 库。我不知道我们应该在 cv2.VideoCapture() 括号中放什么,它应该打开 android 手机摄像头,以便现在在我放的窗口上运行0在括号中

现在我被困在 APK 中,它在点击它的图标时立即崩溃。因为在使用 > Buildozer 创建 APK 时没有显示错误。我搜索了很多来解决问题,但没有成功。我发现了很多与同一问题相关的问题,但找不到解决方案。

main.py 代码如下

from kivymd.app import MDApp
from kivy.lang import Builder
from kivymd.uix.dialog import MDDialog
from kivymd.uix.button import MDFlatButton
from kivy.uix.screenmanager import ScreenManager, Screen
from kivy.core.window import Window
from kivy.graphics.texture import Texture
from kivy.clock import Clock
import cv2

Window.size = (350, 600)

class LoginScreen(Screen):
    pass


class RegistrationScreen(Screen):
    pass


class HomeScreen(Screen):
    pass


class WebCamScreen(Screen):
    flag = None

    def do_start(self):
        flag = False
        self.capture = cv2.VideoCapture(0)
        Clock.schedule_interval(self.load_video, 1.0 / 24.0)

    def load_video(self, *args):
        ret, frame = self.capture.read()
        self.image_frame = frame
        # frame = frame[220:220+250, 400:400+250, :]
        buffer = cv2.flip(frame, 0).tostring()
        image_texture = Texture.create(size=(frame.shape[1], frame.shape[0]), colorfmt="bgr")
        image_texture.blit_buffer(buffer, colorfmt="bgr", bufferfmt="ubyte")
        self.ids.img.texture = image_texture

    def capture_image(self):
        try:
            image_name = "first_pic.jpg"
            cv2.imwrite(image_name, self.image_frame)
        except AttributeError:
            self.flag = MDDialog(
                title="WARNING!",
                text="Please Start the Camera First...!",
                buttons=[
                    MDFlatButton(
                        text="Okay",
                        theme_text_color="Custom",
                        text_color="orange",
                        on_release=self.close_dialog
                    ),
                ],
            )
            self.flag.open()

    def close_dialog(self, obj):
        self.flag.dismiss()


class MainApp(MDApp):

    def build(self):
        screen_manger = ScreenManager()
        screen_manger.add_widget(LoginScreen(name="login"))
        screen_manger.add_widget(RegistrationScreen(name="registration"))
        screen_manger.add_widget(HomeScreen(name="home"))
        screen_manger.add_widget(WebCamScreen(name="camera"))

        return screen_manger


if __name__ == "__main__":
    MainApp().run()

千伏代码

<LoginScreen>
    MDFloatLayout:               
        MDLabel:
            text: "Sign In"
            pos_hint: {"center_x": 0.35, "center_y": 0.7}
            size_hint: (0.5, 0.5)
            font_style: "H4"
            
        MDTextField
            hint_text: "Username"
            mode: "rectangle"
            pos_hint: {"center_x": 0.5, "center_y": 0.6}
            size_hint_x: 0.8
            icon_left: "account-circle"

        MDTextField
            hint_text: "Password"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "key-variant"
            pos_hint: {"center_x": 0.5, "center_y": 0.47}
        
        MDFlatButton:
            text: "Forget Password?"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.74, "center_y": 0.39}
        
        MDRaisedButton:
            text: "Sign In"
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            on_release:
                root.manager.transition.direction = "left"
                root.manager.current = "home"
            
        MDLabel:
            text: "Don't have an account?"
            size_hint: (0.5, 0.5)
            font_style: "Caption"
            pos_hint: {"center_x": 0.49, "center_y": 0.15}
        
        MDFlatButton:
            text: "Register"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.7, "center_y": 0.15}
            on_release:
                root.manager.transition.direction = "left"
                root.manager.current = "registration"
            
<RegistrationScreen>
    MDFloatLayout:               
        MDLabel:
            text: "Sign Up"
            pos_hint: {"center_x": 0.35, "center_y": 0.75}
            size_hint: (0.5, 0.5)
            font_style: "H4"
            
        MDTextField
            hint_text: "Username"
            mode: "rectangle"
            pos_hint: {"center_x": 0.5, "center_y": 0.65}
            size_hint_x: 0.8
            icon_left: "account-circle"

        MDTextField
            hint_text: "Phone Number"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "dialpad"
            pos_hint: {"center_x": 0.5, "center_y": 0.53}
        
        MDTextField
            hint_text: "Password"
            mode: "rectangle"
            size_hint_x: 0.8
            password: True
            icon_left: "key-variant"
            pos_hint: {"center_x": 0.5, "center_y": 0.41}
               
        MDRaisedButton:
            text: "Sign Up"
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.3}
            
        MDLabel:
            text: "Already have an account?"
            size_hint: (0.5, 0.5)
            font_style: "Caption"
            pos_hint: {"center_x": 0.49, "center_y": 0.15}
        
        MDFlatButton:
            text: "Sign In"
            theme_text_color: "Custom"
            text_color: "orange"
            pos_hint: {"center_x": 0.75, "center_y": 0.15}
            on_release:
                root.manager.transition.direction = "right"
                root.manager.current = "login"
                
<HomeScreen>
    MDFloatLayout: 
        MDRaisedButton:
            text: "Register Your Face"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.85}
            on_release:
                root.manager.transition.direction = "left" 
                root.manager.current = "camera" 
         
        MDRaisedButton:
            text: "Compare Faces"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.7}
        
        MDRaisedButton:
            text: "Remove Face Data"
            size_hint: (0.7, 0.05)
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.55}
    
<WebCamScreen>
    MDFloatLayout:
        MDRaisedButton:
            text: "Start Camera"
            size_hint_x: None
            size_hint_y: None
            md_bg_color: "orange"
            pos_hint: {"center_x": 0.5, "center_y": 0.95}
            on_release:
                root.do_start()
            
        Image:
            id: img
            size_hint_x: 0.85
            size_hint_y: 0.5
            pos_hint: {"center_x": 0.5, "center_y": 0.6}
            
        MDIconButton:
            icon: "camera"
            md_bg_color: "orange"
            pos_hint: {"center_x": .5, "center_y": .2}
            on_release:
                root.capture_image()               

我正在做谷歌实验室关注this 博客2. Google Colab 方式!.我在 Windows 11 上运行它。

和我的构建器.spec代码是

# (str) Title of your application
title = Face Recognizer

# (str) Package name
package.name = myapp

# (str) Package domain (needed for android/ios packaging)
package.domain = org.test

# (str) Source code where the main.py live
source.dir = .

# (str) Application versioning (method 1)
version = 0.1

# (list) Application requirements
# comma separated e.g. requirements = sqlite3,kivy
requirements = python3,kivy,kivymd,opencv,Pillow,urllib3,Kivy-Garden,numpy

# (str) Supported orientation (one of landscape, sensorLandscape, portrait or all)
orientation = portrait

# change the major version of python used by the app
osx.python_version = 3

# Kivy version to use
osx.kivy_version = 1.9.1

# (bool) Indicate if the application should be fullscreen or not
fullscreen = 0

# (list) Permissions
android.permissions = CAMERA

# (str) Path to a custom kivy-ios folder
#ios.kivy_ios_dir = ../kivy-ios
# Alternately, specify the URL and branch of a git checkout:
ios.kivy_ios_url = https://github.com/kivy/kivy-ios
ios.kivy_ios_branch = master

# Another platform dependency: ios-deploy
# Uncomment to use a custom checkout
#ios.ios_deploy_dir = ../ios_deploy
# Or specify URL and branch
ios.ios_deploy_url = https://github.com/phonegap/ios-deploy
ios.ios_deploy_branch = 1.10.0

# (bool) Whether or not to sign the code
ios.codesign.allowed = false

# (int) Log level (0 = error only, 1 = info, 2 = debug (with command output))
log_level = 2

# (int) Display warning if buildozer is run as root (0 = False, 1 = True)
warn_on_root = 1

是否必须将库/包的版本放在 buildozer.spec 文件中,如 (kivymd==this_version)。

提前致谢 :) 我将非常感谢解决我这个问题的答案<3。

【问题讨论】:

  • 欢迎。 tourHow to Askminimal reproducible example。需要堆栈跟踪。调试你的代码。
  • @ChristophRackwitz 先生我应该如何调试代码我的意思是我在 PyCharm 上做这个所以如果你告诉我调试方法......!或者给我一些我应该学习的参考表格。我

标签: python android kivy kivymd buildozer


【解决方案1】:

您可以使用 logcat 来调试您的应用程序,在 google 中找到如何使用它!

【讨论】:

    猜你喜欢
    • 2021-10-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-08
    • 1970-01-01
    • 1970-01-01
    • 2013-07-07
    • 2022-10-02
    相关资源
    最近更新 更多