【问题标题】:ANativeWindow_fromSurface(env,surface) always returns nullANativeWindow_fromSurface(env,surface) 总是返回 null
【发布时间】:2021-05-23 12:07:04
【问题描述】:

我正在尝试将表面从片段传递到我的本机代码。

但是当我使用 ANativeWindow_fromSurface(env,surface); 创建时,ANativeWindow 始终为空

我使用的是 kotlin 而不是 java。

我从片段中的 onViewCreated() 函数调用本机函数:

override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
    super.onViewCreated(view, savedInstanceState)

    

    surfaceView  = view.findViewById(R.id.surfaceView)
    surface = surfaceView.holder.surface

    myClass = MyClass()
    myClass.myFunction(surface)
 
}

我的班级:

class MyClass{

init {
    System.loadLibrary("nativeLib")
}

external fun myFunction(surface: Surface): Int

}

这是我的 native.cpp :

extern "C"
JNIEXPORT jint JNICALL
Java_com_my_app_MyClass_myFunction(JNIEnv *env,jobject obj,jobject surface){


nativeWindow = ANativeWindow_fromSurface(env,surface);

if(nativeWindow == nullptr){
    ALOG("nativeWindow is null");
    return -1;
}

}

当我安装我的应用程序时,它显示 nativeWindow 为空。 为什么它是空的? 谁能建议我解决这个问题?

【问题讨论】:

    标签: android c android-ndk java-native-interface surfaceview


    【解决方案1】:

    终于找到解决办法了!!

    我将 myClass.myFunction() 调用移至 SurfaceHolder.Callback{}。

    现在看起来像这样:

    override fun surfaceCreated(p0: SurfaceHolder) {
    
        object : Thread(){
            override fun run() {
                myClass.myFunction(surface)
            }
        }.start()
    
    }
    

    【讨论】:

      猜你喜欢
      • 2017-08-31
      • 2018-03-05
      • 1970-01-01
      • 2014-03-04
      • 2016-11-02
      • 2014-05-06
      • 2012-06-05
      • 2014-05-30
      相关资源
      最近更新 更多