Kivy:[WindowSDL] 找不到正在运行的应用程序,退出

问题描述

我在图库中使用 jnius 和意图在 kivy 中做图像选择器 然后我点击按钮打开图库,应用程序在我得到的 logcat 中关闭

05-07 15:27:19.507 30865 30865 I python : [INFO] [WindowSDL] 未找到正在运行的应用程序,退出 05-07 15:27:19.508 30865 30865 I python:[INFO] [Base] 正在离开应用程序... 05-07 15:27:19.514 30865 30923 I python:[INFO] [WindowSDL] 退出主循环并关闭

05-07 15:27:19.570 30865 30923 I python:回溯(最近一次调用): 05-07 15:27:19.570 30865 30923 I python:文件“/content/.buildozer/android/app/main.py”,第 103 行,

05-07 15:27:19.571 30865 30923 我 python:AttributeError: 'nonetype' 对象没有属性 'run'

05-07 15:27:19.571 30865 30923 I python:Android 的 Python 结束了。 这是我的代码:-

from android.runnable import run_on_ui_thread as run_thread
from jnius import autoclass,cast
from kivy.uix.widget import Widget
from kivy.core.window import Window
from kivy.uix.gridlayout import GridLayout
from kivy.uix.button import Button
from kivy.app import runTouchApp
from kivy.clock import Clock,mainthread
from android import activity as act
    
m=GridLayout(cols=1,rows=1)
d=GridLayout(cols=1,rows=1)
m.add_widget(d)
Uri = autoclass('android.net.Uri')

# Value of MediaStore.Images.Media.DATA
MediaStore_Images_Media_DATA = "_data"

# Custom request codes
RESULT_LOAD_IMAGE = 1
PythonActivity = autoclass('org.kivy.android.PythonActivity')
WebViewA = autoclass('android.webkit.WebView')
Intent = autoclass('android.content.Intent')
Activity = autoclass('android.app.Activity')
WebViewClient = autoclass('android.webkit.WebViewClient')
LinearLayout = autoclass('android.widget.LinearLayout')
Context = autoclass('android.content.Context')
Uri = autoclass('android.net.Uri')
ViewGroup = autoclass('android.view.ViewGroup')
LayoutParams = autoclass('android.view.ViewGroup$LayoutParams')
# Activity is only used to get these codes. Could just hardcode them.
    # /** Standard activity result: operation canceled. */
    # public static final int RESULT_CANCELED    = 0;
    # /** Standard activity result: operation succeeded. */
    # public static final int RESULT_OK           = -1;
    # /** Start of user-defined activity results. */
    # Not sure what this means
    # public static final int RESULT_FirsT_USER   = 1;
@run_thread
def Push(callback):
    """Open gallery Activity and call callback with absolute image filepath of image user selected.
    None if user canceled.
    """
    activity1 = cast('android.app.Activity',PythonActivity.mActivity)
    # PythonActivity.mActivity is the instance of the current Activity
    # BUT,startActivity is a method from the Activity class,not from our
    # PythonActivity.
    # We need to cast our class into an activity and use it
    # Forum discussion: https://groups.google.com/forum/#!msg/kivy-users/bjsG2j9bptI/-Oe_aGo0newJ
    def on_activity_result(request_code,result_code,intent):
        if request_code != RESULT_LOAD_IMAGE:
            print("not selected")
            return

        if result_code == Activity.RESULT_CANCELED:
            Clock.schedule_once(lambda dt: callback(None),0)
            return

        if result_code != Activity.RESULT_OK:
            # This may just go into the void...
            raise NotImplementedError('UnkNown result_code "{}"'.format(result_code))

        selectedImage = intent.getData() # Uri
        filePathColumn = [MediaStore_Images_Media_DATA] # String[]
        # Cursor
        cursor = currentActivity.getContentResolver().query(selectedImage,filePathColumn,None,None)
        cursor.movetoFirst()

        # int
        columnIndex = cursor.getColumnIndex(filePathColumn[0])
        # String
        picturePath = cursor.getString(columnIndex)
        cursor.close()
        print('android_ui: user_select_image() selected '+picturePath)
        parent = cast(ViewGroup,layout.getParent())
        if parent is not None:parent.removeView(layout)
        webview.clearHistory()
        webview.clearCache(True)
        webview.clearFormData()
        webview.destroy()
        layout = None
        webview = None


    # See: http://pyjnius.readthedocs.org/en/latest/android.html
    act.bind(on_activity_result=on_activity_result)

    intent= Intent(Intent.ACTION_GET_CONTENT)
    intent.addCategory(Intent.CATEGORY_OPENABLE)
    intent.setType("image/*")
    intent.setAction(Intent.ACTION_GET_CONTENT)
    

    # http://programmerguru.com/android-tutorial/how-to-pick-image-from-gallery/
    # http://stackoverflow.com/questions/18416122/open-gallery-app-in-android

    # Todo setType(Image)?
    activity1.startActivityForResult(intent,RESULT_LOAD_IMAGE)
b=Button(text="hi")
b.bind(on_press=Push)
d.add_widget(b)
runTouchApp(m)

提前致谢

解决方法

添加类并将其更改为 App().run() 后,上面的评论可以正常工作,谢谢