Quantcast
Channel: Cocos中文社区 - 最新主题
Viewing all articles
Browse latest Browse all 88774

Java 回调 lua function 问题

$
0
0

@HuaxingZheng 写道:

目前是这样的一个需求

打开相册,并传回选择的照片资源fullpath

我利用luaj调用java函数openSystemGalleryForSelect
并向java传递了一个lua处理函数,这个处理函数在Java代码中没有立即调用,而是retain了一下
等待处理结束后,在另外一个Java函数onImageSelected中回调。 遇到的问题是,Java函数中img变量的值是正确的,而调用lua function 时,打印的内容却是空值, 而不是">>>>imagePath"这种形式

-- xx 为我自己封装的库
xx.Helper:openSystemGalleryForSelect(function(imagePath)
    xx.ui.Hint.showMsg(">>>>imagePath is:"..imagePath)
end)

-- xx库中的该方法实现如下:
local openSystemGalleryForSelect
openSystemGalleryForSelect = function(self, callback) 
    local args = {callback}
    local sigs = "(I)V"
    local ok = luaj.callStaticMethod(className,"openSystemGalleryForSelect",args,sigs)
    if not ok then 
        -- 调用失败
    end 
end

Java代码如下:

// 打开系统相册 选择图片后回调
    public static void openSystemGalleryForSelect(final int luaFunc) {
        if (_select_photo_lua_func != 0){
            Cocos2dxLuaJavaBridge.releaseLuaFunction(_select_photo_lua_func);
            _select_photo_lua_func = 0;
        }
        _select_photo_lua_func = luaFunc;
        Cocos2dxLuaJavaBridge.retainLuaFunction(_select_photo_lua_func);

        Intent intent = new Intent(Intent.ACTION_PICK, null);
        intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, IMAGE_UNSPECIFIED);
        activity.startActivityForResult(intent, PHOTOSELECT);
    }
    
// 选择完照片的回调
public static void onImageSelected(final String img) {
        if (_select_photo_lua_func != 0){
            Cocos2dxGLSurfaceView.getInstance().queueEvent(new Runnable() {
                @Override
                public void run() {
                    Cocos2dxLuaJavaBridge.callLuaFunctionWithString(_select_photo_lua_func, img);
                    Cocos2dxLuaJavaBridge.releaseLuaFunction(_select_photo_lua_func);
                    _select_photo_lua_func = 0;
                }
            });
        }
    }

有人可以解惑?

帖子: 1

参与者: 1

阅读整个主题


Viewing all articles
Browse latest Browse all 88774

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>