Crash when attempting to use JNIEnv FindClass

1,540 views
Skip to first unread message

Alan Ide

unread,
Feb 16, 2014, 11:38:24 AM2/16/14
to andro...@googlegroups.com
I am attempting to display the soft keyboard in an NDK application. I do not have much experience with JNI, and seem to be doing something wrong with FindClass() but I have not been able to determine what that is. Any help pointing me in the right direction would be much appreciated.

The application is entirely native, with only this dummy activity to load my shared libraries. The main.cpp populates a global static android_app* that I am then attempting to use later to call my show keyboard method. I have removed all the code that does not pertain to the issue, and have verified that the application works as intended when not attempting to call FindClass().


Java Code:

package com.digitalsynapses.engine;

import android.app.Activity;
import android.content.Intent;
import android.content.Context;
import android.os.Bundle;
import android.view.inputmethod.InputMethodManager;

public class DummyActivity extends Activity
{
    static DummyActivity da;
    
    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        System.loadLibrary("openal");
        System.loadLibrary("ds_engine");
        
        super.onCreate(savedInstanceState);
        
        Intent intent = new Intent(DummyActivity.this, android.app.NativeActivity.class);
        DummyActivity.this.startActivity(intent);
        
        da = this;
    }
}

class Keyboard
{
    static void show()
    {
        InputMethodManager m = (InputMethodManager)DummyActivity.da.getSystemService(Context.INPUT_METHOD_SERVICE);
        m.toggleSoftInput(InputMethodManager.SHOW_FORCED, 0);
    }
    
    static void hide()
    {
        InputMethodManager m = (InputMethodManager)DummyActivity.da.getSystemService(Context.INPUT_METHOD_SERVICE);
        m.toggleSoftInput(0, 0);
    }
}


C++ Code: AppController.h

//Global class holding static NDK variables
class AppController
{
public:
    //Data - Static shared
    static android_app* sharedApplication;
};
 

C++ Code: Main.cpp: 

//Main android NDK interface 
void android_main(android_app* pApplication)
{
//Setup main android application handler
AppController::sharedApplication = pApplication;

                  //** Other setup stuff **//
          }


C++ Code: Method to display soft keyboard:

JNIEnv* env = nullptr;
AppController::sharedApplication->activity->vm->AttachCurrentThread(&env, NULL);

jclass keyboard = env->FindClass("com/digitalsynapses/engine/Keyboard"); //<- Crashes when calling this method.
jmethodID show = env->GetStaticMethodID(keyboard, "show", "()V");
env->CallStaticVoidMethod(keyboard, show);
        
AppController::sharedApplication->activity->vm->DetachCurrentThread();


NoAngel

unread,
Feb 18, 2014, 4:43:02 AM2/18/14
to andro...@googlegroups.com
1. public class DummyActivity extends Activity
shouldnt it be "extends NativeActivity"?

2.jclass keyboard = env->FindClass("com/
digitalsynapses/engine/Keyboard"); //<- Crashes when calling this method.
yep, coz there's no Keyboard class in com/digitalsynapses/engine
u'll get java.lang.ClassNotFound exception. fix your code

3. calling java from c++ isnt a trivial task. see my topic here: http://stackoverflow.com/questions/10184527/android-calling-java-class-from-c-native-activity

2014年2月17日月曜日 1時38分24秒 UTC+9 Alan Ide:
Reply all
Reply to author
Forward
0 new messages