英文:
Unresolved reference in Kotlin using JNI-Bridge
问题
我想在Kotlin的可组合函数中调用startOsc()
函数,但出现了这个错误:Unresolved reference: startOsc
这是我的Kotlin代码:
class MainActivity : ComponentActivity() {
private external fun startEngine()
private external fun stopEngine()
private external fun startOsc() // <-- 这里定义了函数
companion object {
init {
System.loadLibrary("pitch_andoid")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TwoNotesView()
}
startEngine()
}
override fun onDestroy() {
super.onDestroy()
stopEngine()
}
}
@Composable
fun TwoNotesView() {
Column(
modifier = Modifier
.fillMaxSize()
.background(colorResource(id = R.color.cyan))
) {
Row {
Box (
modifier = Modifier
.clickable {
startOsc() // <-- 这是错误的那一行
}
) {
AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // 这是其他可组合部分,我为简单起见省略了
}
}
}
}
CMakeLists.txt可能是正确的,因为startEngine()
和stopEngine()
没有未解决的引用。这是startOsc()
函数的jni-bridge.cpp代码:
JNIEXPORT void JNICALL
Java_com_example_pitch_1andoid_MainActivity_startOsc(JNIEnv *env, jobject/* this */) {
audioEngine->setToneOn(true);
}
我拼写错误了项目的名称,这就是为什么所有东西都叫做"andoid"而不是"android"。抱歉。
编辑:错误应该是将函数从MainActivity传递到可组合函数中。我尝试在MainActivity中调用函数,并没有出错。
英文:
I want to call the startOsc()
function inside of a Composable in Kotlin and get this error: Unresolved reference: startOsc
This is my Kotlin code:
class MainActivity : ComponentActivity() {
private external fun startEngine()
private external fun stopEngine()
private external fun startOsc() // <-- here the function is defined
companion object {
init {
System.loadLibrary("pitch_andoid")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TwoNotesView()
}
startEngine()
}
override fun onDestroy() {
super.onDestroy()
stopEngine()
}
}
@Composable
fun TwoNotesView() {
Column(
modifier = Modifier
.fillMaxSize()
.background(colorResource(id = R.color.cyan))
) {
Row {
Box (
modifier = Modifier
.clickable {
startOsc() // <-- this is the line of the error
}
) {
AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // this is some other composable I left out for simplicity
}
}
}
}
The CMakeLists.txt is probably correct, since there is no Unresolved Reference for startEngine()
and stopEngine()
.
Here is the code of the jni-bridge.cpp for the startOsc()
function:
JNIEXPORT void JNICALL
Java_com_example_pitch_1andoid_MainActivity_startOsc(JNIEnv *env, jobject/* this */) {
audioEngine->setToneOn(true);
}
I made a spelling mistake naming the project, that's why everything is called "andoid" instead of "android", Sorry.
edit: the error has to be passing down the function from MainActivity to the Composable. I tried calling the function inside MainActivity and got no error.
答案1
得分: 0
通过以下方式传递 startOsc()
来解决这个问题:
class MainActivity : ComponentActivity() {
private external fun startEngine()
private external fun stopEngine()
external fun startOsc() // <-- 删除 'private'
companion object {
init {
System.loadLibrary("pitch_andoid")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TwoNotesView(::startOsc) // <-- 作为参数传递 startOsc()
}
startEngine()
}
override fun onDestroy() {
super.onDestroy()
stopEngine()
}
}
@Composable
fun TwoNotesView(startOsc: () -> Unit) { // <-- 添加 startOsc() 作为参数
Column(
modifier = Modifier
.fillMaxSize()
.background(colorResource(id = R.color.cyan))
) {
Row {
Box (
modifier = Modifier
.clickable {
startOsc()
}
) {
AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // 这是我为简单起见略去的另一个可组合部分
}
}
}
}
英文:
Solved it by passing down startOsc()
like this:
class MainActivity : ComponentActivity() {
private external fun startEngine()
private external fun stopEngine()
external fun startOsc() // <-- deleted 'private'
companion object {
init {
System.loadLibrary("pitch_andoid")
}
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContent {
TwoNotesView(::startOsc) // <-- pass down startOsc() as parameter
}
startEngine()
}
override fun onDestroy() {
super.onDestroy()
stopEngine()
}
}
@Composable
fun TwoNotesView(startOsc: () -> Unit) { // <-- add startOsc() as parameter
Column(
modifier = Modifier
.fillMaxSize()
.background(colorResource(id = R.color.cyan))
) {
Row {
Box (
modifier = Modifier
.clickable {
startOsc()
}
) {
AnswerButton(ImageSource = R.drawable.round_replay_24_cyan) // this is some other composable I left out for simplicity
}
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论