如何在JNI中从C ++调用Java方法(How to call Java methods from C++ in JNI)
所以我正在编写一个使用大型c ++库的Android应用程序。 我有一切工作,以便java应用程序可以调用c ++委托方法,但我发现自己希望我可以将消息从c ++记录到Android日志。 这很容易从java中,但我不知道如何从c ++调用java方法。 我的搜索找到了从c ++中打开jvm的方法,这完全不是我想要做的。 理想情况下,我想将一个日志方法指针传递给c ++,然后可以随时使用它。 当然,java不支持方法指针。 我的java方法看起来像这样:
private void log(String s){ Log.i(Tag, s); // Android log }
我只是不知道如何让c ++来访问这个方法。
So I'm writing an Android app which uses a large c++ library. I have everything working so that the java app can call the c++ delegation methods, but I'm finding myself wishing I could log messages from c++ to the Android log. This is easy from java, but I'm at a loss as to how to call a java method from c++. My searches found methods for opening a jvm from c++, which is not at all what I want to do. Ideally, I'd like to pass a log method pointer to c++, which could then be used whenever I wanted. Of course, java doesn't support method pointers. My java method would look something like:
private void log(String s){ Log.i(Tag, s); // Android log }
I just don't know how to allow c++ to access this method.
原文:https://stackoverflow.com/questions/3902066
最满意答案
迭代器(在你的情况下
int i
)每次迭代增加1。 如果幻数无效,您可以将迭代器递减1。if (guess > randomNumber && guess <= 100){ System.out.println("Your guess, "+guess+", is greater than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 } else if (guess < randomNumber && guess > 0){ System.out.println("Your guess, "+guess+", is less than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 }
The iterator (in your case
int i
) increment by 1 for each iteration. You can decrement your iterator by 1 if the magic number is invalid.if (guess > randomNumber && guess <= 100){ System.out.println("Your guess, "+guess+", is greater than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 } else if (guess < randomNumber && guess > 0){ System.out.println("Your guess, "+guess+", is less than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now the value of i is 0. The value of i in the next iteration will be 1 }
相关问答
更多-
下列中不属于面向对象的编程语言的是?[2022-05-30]
a -
猜测数字的程序(Program which guesses a number)[2022-12-17]
你使用scan.next(); 。 对于输入too much ,这也只会返回。 要读取整行,您需要使用scan.nextLine(); You use scan.next();. For input too much, that will return only too. To read the whole line, you need to use scan.nextLine(); -
迭代器(在你的情况下int i )每次迭代增加1。 如果幻数无效,您可以将迭代器递减1。 if (guess > randomNumber && guess <= 100){ System.out.println("Your guess, "+guess+", is greater than the magic number."); // If the value of i at this point is 1 i--; //Decrement i by 1 // Now t ...
-
y从1开始,并且从不小于1,因此minGuess总是1。 for (int y = 1; y <= 5; y= y + 1) { ... if (y < minGuess) minGuess = y; ... } 考虑只有在猜测成功后才更新minGuess和maxGuess。 y starts at one, and is never less than one, thus mi ...
-
请注意,我的建议不是一个优雅的解决方案。 用值(例如15)创建一个静态最终的maxGuesses。 将while-condition从!word.equals(hiddenWord)更改为true 。 这个循环现在不会终止 - 除非我们引入break语句。 创建一个新的检查(if-condition)来查看是否超过了尝试次数(例如missCount>maxGuesses )。 在if块中打印这些信息,并添加break来退出inner。 用word.equals(hiddenWord)创建另一个检查(if-c ...
-
我允许3次猜测还是2次?(Am I allowing 3 guesses or 2?)[2022-12-07]
条件应该是: while count < 3: 为了使它更容易理解,我建议你在count = 1启动计数器并写下如下条件: while count <= 3: 现在更清楚的是,允许3次重复。 但让我们看看为什么你的代码是错误的: count从0开始,并且0 < 2是真的,所以我们进入循环 在第一次失败的尝试中, count增加到1 ,并且1 < 2是真的,所以我们再次进入循环 在第二次失败的尝试中, count增加到2 ,并且2 < 2不再是真的,所以我们退出循环 所以你看,只有两次尝试被考虑。 The ... -
我很难在Hangman游戏中输出用户的猜测数量(I'm having trouble outputing the user's amount of guesses in a Hangman game)[2022-06-17]
这里有一个错误: if (Character.toLowerCase(newWord.charAt(x)) != letterInput.charAt(0)) { guesses= guesses - 1; System.out.println("Your letter was not found in spaces you ... -
使用||(Using || in if statements)[2023-09-08]
而不是isset()在这种情况下使用empty()作为一个不存在的值对你没有任何好处,即。 test.php?number=没有为数字输入值 。 同上的信 。 以下将检查数字或字母是否包含条件将满足的值,并将打印Hello
。 否则,如果两者都不包含值,您将被重定向。 if ( ! empty($_GET['number']) || ! empty($_GET['letter']) ) { echo 'Hello
'; } else { header('Lo ... -
试图显示在Hangman游戏中留下的猜测量(trying to display the amount of guesses that is left in a Hangman game)[2023-08-26]
要显示剩余的猜测量,您需要有一个带有id的范围,然后每当您减少猜测时,将该值放入范围的text属性中。 HTML: jQuery的: $("#guessesLeft").text(guessesLeft); To display the amount of guesses left, you will need to have a span with an id, then whenever you decrement the guesses ... -
你在onClick函数中第二次实例化ranNum。 摆脱这个。 第二:你应该在你的onClick功能外面创建一个计数器,计算用户的次数,并在每次用户点击时检查计数器。 编辑:你已经有了你的计数器,你应该只在尝试时检查用户输入 public class Task1Activity extends AppCompatActivity { int attempts = 0; final int maxAttempts = 3; Random randGen = new Random(); ...