Dialog not displaying when calling from JNI
来源:互联网 发布:留学回国证明 知乎 编辑:程序博客网 时间:2024/06/05 21:49
这是一篇非常有用的问答,将其复制到这里。
Dialog not displaying when calling from JNI
I have a native C
function (with JNI of course) that should callback a java method to display a dialog but this dialog is never displayed.
This is how this works:
- C code calls a java method
- The java method logs something
- Then it calls
showDialog(SOME_ID)
- The
onCreateDialog(SOME_ID)
is called - The dialog is constructed using
AlertDialog.Builder
- When I call
AlertDialog alert = builder.create()
the application just stops right there (without crashing or freezing) butonly when this call starts from the C code.
The code is as follows:
@Overrideprotected Dialog onCreateDialog(int id) { switch (id) { case SOME_ID: AlertDialog.Builder builder = new AlertDialog.Builder(this); builder.setMessage("Message"); builder.setPositiveButton(...); builder.setNegativeButton(...); AlertDialog alert = builder.create(); System.out.println("...Never called from JNI"); return alert; } return null;}
In 6 I mean that I log something before and after callingbuilder.create()
but the log after the create
method is never called so the method never returns and the dialog is never created. I have a button to trigger the dialog and, as expected, it works.
I did another test, I printed getApplicationContext()
calling directly fromonCreate()
and from onCreateDialog()
(called from C code) and it is the same, because one of the first things I thought was that the onCreateDialog() was being called from another Context or something.
Do you have any idea why the dialog is not displaying?
1 Answer
The problem was that when calling back from JNI the application wasn't on UI Thread so the dialog was never shown. To fix it I used a Handler (on Activity):
public final Handler dialogHandler = new Handler(){ public void handleMessage(Message msg){ showDialog(SOME_ID); } };
So instead of directly call showDialog()
I do it by calling dialogHandler.sendMessage()
.
Not the answer you're looking for? Browse other questions taggedandroidjni or ask your own question.
- Dialog not displaying when calling from JNI
- Considerations when calling Java from PeopleCode
- TypeError: $ is not a function when calling jQuery function
- http://stuf.ro/calling-c-code-from-java-using-jni
- Calling Java Class Methods from C with JNI
- NullPointerException when calling getReadableDatabase()
- Calling JNI Functions with Java Object Arguments from the Command Line
- Scrolling a UITableView When Displaying the Keyboard
- Scrolling a UITableView When Displaying the Keyboard
- Calling Wicket from Javascript,
- Calling Ruby From C
- calling a host function from a __device__/__global__ function is not allowed
- calling a __host__ function("_wassert") from a __global__ function is not allowed错误
- calling a __host__ function("XXX") from a __global__ function is not allowed
- When not now ,When?
- Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find
- Last_IO_Error: Got fatal error 1236 from master when reading data from binary log: 'Could not find
- Android – Displaying Dialogs From Background Threads
- MySql可视化工具MySQL Workbench使用教程
- 指针函数的返回值要求
- DatePickerDialog.OnDateSetListener 译文
- dynamic web module和对应的TOMCAT支持的版本
- 为Android加入busybox工具
- Dialog not displaying when calling from JNI
- 技术说明书模板
- java的反射与代理
- 国内银行应用软件项目外包模式探讨
- vi命令大全
- Struts2原来是那么的简单!
- [Java]读取文件方法大全
- 黑马程序员-银行业务调度系统
- 黑马程序员----JAVA----集合小结(Set)