【问题标题】:Kivy on android: Exception in parser.py create_missing: TypeError: Argument 'name' has incorrect type (expected str, got unicode)android上的Kivy:parser.py create_missing中的异常:TypeError:参数'name'的类型不正确(预期的str,得到unicode)
【发布时间】:2018-01-28 13:23:03
【问题描述】:

我的 Kivy 应用在 Android 上立即崩溃。 我想这是一些 Python2/Python3 不兼容,但我不知道这个错误的实际原因是什么(adb logcat 输出):

01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/app/main.py", line 5, in <module>
01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/app.py", line 802, in run**strong text**
01-28 14:00:39.421  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/app/pfadfinder/hauptmenue.py", line 152, in build
01-28 14:00:39.422  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/builder.py", line 382, in load_string
01-28 14:00:39.422  7457  7492 I python  :    File  "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/builder.py", line 496, in _apply_rule
01-28 14:00:39.422  7457  7492 I python  :    File "/home/kivy/Pfadfinder/.buildozer/android/platform/build/dists/pfadfinder/private/lib/python2.7/site-packages/kivy/lang/parser.py", line 281, in create_missing
01-28 14:00:39.423  7457  7492 I python  :  TypeError: Argument 'name' has incorrect type (expected str, got unicode)

该应用是在 Windows 上使用 Python 3.6 开发的。

在 Android 上,它在 Python 2.7.2 (Kivy v1.10.0) 中运行。

注意:我是德国人,所以该应用程序应与变音符号 ä ö ü 一起使用。 我正在像这样加载 KV 文件:

def build(self):
    with io.open(os.path.join(THIS_DIR, "hauptmenue.kv"), encoding='utf-8') as f:
       screen_management = Builder.load_string(f.read())

最后一行是堆栈跟踪中提到的第 152 行。

KV 文件如下所示:

:kivy 1.1.0

ScreenManagement:
    hauptmenue: hauptmenue
    MainMenu:
        id: hauptmenue

<LevelButton>:
    font_size: "40dp"


<MainMenu>:
    name: "hauptmenue"

    level_auswahl: level_auswahl

    BoxLayout:
        orientation: "vertical"
        size: root.size

        Label:
            text: "Pfadfinder"
            size_hint: (1.0, None)
            font_size: "30dp"
            height: "40dp"

        GridLayout:
            id: level_auswahl
            rows: 6
            cols: 5

        BoxLayout:
            orientation: "horizontal"
            size_hint: (1.0, None)
            height: "40dp"
            Button:
                text: "Einstellungen"
                on_release: app.open_settings()
                font_size: "30dp"
            Button:
                text: "Beenden"
                on_release: app.stop()
                font_size: "30dp"

<Einstellungen>:
    name: "einstellungen"
    Settings:
        on_close: app.close_settings()

【问题讨论】:

    标签: android python kivy kivy-language


    【解决方案1】:

    我会回答我自己的问题。

    错误消息确实没有多大帮助,所以我试着记住自上一个工作版本以来发生了什么变化。

    加载 KV 文件的方法没有改变 - 这几天前就是这样 - 原因是 KV 文本中的变音符号在 Windows 上没有正确呈现。

    我添加了屏幕管理、应用配置和 JsonStore,例如为玩家的昵称。 当然,这个名字我用了变音符号,所以我认为这些是罪魁祸首。 但这条路走错了。

    原因实际上是我在 KV 文件中有一个屏幕的“名称”属性。

    实际上,帮助修改以依赖 Python 版本的方式加载 KV 文件的方法:

    fname = os.path.join(THIS_DIR, "hauptmenue.kv")
    if sys.version_info[0] >= 3:
        with io.open(fname, encoding='utf-8') as f:
            screen_management = Builder.load_string(f.read())
    else:
        screen_management = Builder.load_file(fname)
    

    希望这对其他人有所帮助...

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多