【问题标题】:Kivy: [WindowSDL] No running App found, exitKivy:[WindowSDL] 未找到正在运行的应用程序,退出
【发布时间】:2021-07-29 15:19:44
【问题描述】:

我通过使用 jnius 和 Intents 在画廊中的 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 : Traceback(最近一次调用最后一次): 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 I python: AttributeError: 'NoneType' object has no attribute 'run'

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

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, 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.get_running_app() 方法来获取当前正在运行的App。但是由于您没有运行 App ,因此该代码将失败。尝试在其build() 方法中定义一个返回m 的简单App,并运行App 而不是使用runTouchApp()。此外,runTouchApp() 返回None,因此runTouchApp(m).run() 将失败(在runTouchApp(m) 完成后)。
  • @JohnAnderson 我终于明白了,但是 on_activity_result 上没有显示图像我添加了图像源 = picturePath 它在 logcat 中也没有显示任何错误,为什么?
  • 谢谢我终于明白了

标签: android kivy pyjnius


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 2021-03-15
    • 2019-06-27
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-07
    • 1970-01-01
    • 2015-02-19
    • 1970-01-01
    相关资源
    最近更新 更多