runOnUiThread Crash

Hi, I want to call the java method from c++ level, so I use jni way by JNIHelper.

In my Java class, there is a method like:

private void onCallFromCpp() {
Log.d(“###”, “Call from C++”);
}

this code is work good and i can see the message is print in logcat.

but i changed the method like:

private void onCallFromCpp() {
Log.d(“###”, “Call from C++”);
runOnUiThread(new Runnable() {
public void run() {
// Do nothing here… but crashed…
}
});
}
The message is print in logcat BUT app exited suddenly. I just want to call some method which must be called in Android UI thread.
What should i do?

Can you post a LogCat please?

You could use handlers for running code on the UI thread

5 Jan wrote:

Hi, I want to call the java method from c++ level, so I use jni way by JNIHelper.
>
In my Java class, there is a method like:
>
private void onCallFromCpp() {
Log.d(“###”, “Call from C++”);
}
>
this code is work good and i can see the message is print in logcat.
>
but i changed the method like:
>
private void onCallFromCpp() {
Log.d(“###”, “Call from C++”);
runOnUiThread(new Runnable() {
public void run() {
// Do nothing here… but crashed…
}
});
}
The message is print in logcat BUT app exited suddenly. I just want to call some method which must be called in Android UI thread.
What should i do?

As I known , JNI method should be static , so you should change onCallFromCpp to static , after you change it , you should do another thing : give a activity to that Java class , because runOnUiThread is a method of Activity!

C Zhang wrote:

5 Jan wrote:
> Hi, I want to call the java method from c++ level, so I use jni way by JNIHelper.
>
> In my Java class, there is a method like:
>
> private void onCallFromCpp() {
> Log.d(“###”, “Call from C++”);
> }
>
> this code is work good and i can see the message is print in logcat.
>
> but i changed the method like:
>
> private void onCallFromCpp() {
> Log.d(“###”, “Call from C++”);
> runOnUiThread(new Runnable() {
> public void run() {
> // Do nothing here… but crashed…
> }
> });
> }
> The message is print in logcat BUT app exited suddenly. I just want to call some method which must be called in Android UI thread.
> What should i do?
>
As I known , JNI method should be static , so you should change onCallFromCpp to static , after you change it , you should do another thing : give a activity to that Java class , because runOnUiThread is a method of Activity!

Can you call a non-static method within a static area? runOnUiThread is not a static method!

Cristian Cristea wrote:

You could use handlers for running code on the UI thread

I have tried using a handler, but I was told the Looper.prepare() is not called

5 Jan wrote:

C Zhang wrote:
> 5 Jan wrote:
> > Hi, I want to call the java method from c++ level, so I use jni way by JNIHelper.
> >
> > In my Java class, there is a method like:
> >
> > private void onCallFromCpp() {
> > Log.d(“###”, “Call from C++”);
> > }
> >
> > this code is work good and i can see the message is print in logcat.
> >
> > but i changed the method like:
> >
> > private void onCallFromCpp() {
> > Log.d(“###”, “Call from C++”);
> > runOnUiThread(new Runnable() {
> > public void run() {
> > // Do nothing here… but crashed…
> > }
> > });
> > }
> > The message is print in logcat BUT app exited suddenly. I just want to call some method which must be called in Android UI thread.
> > What should i do?
>
> As I known , JNI method should be static , so you should change onCallFromCpp to static , after you change it , you should do another thing : give a activity to that Java class , because runOnUiThread is a method of Activity!
>
Can you call a non-static method within a static area? runOnUiThread is not a static method!

you can give a reference of activity to your JNIHelper , than you can call runOnUiThread by activity.runOnUiThread!

This SO post claims to include a solution …