u014032613
xiefeng272
采纳率66.7%
2018-01-08 03:45

java通过jni接口向C++中传值问题

5
已采纳

JNI WARNING: instance jfieldID 0x62bb4ff0 not valid for class Ljava/lang/Class; (GetIntField)

in Lcom/xxxx/test/MyActivity;.cmdHUProtoclVersion:(Lcom/xxxx/test/MyActivity$MyVersion;)I (GetIntField)
出现如上的warnning,数值设置不正确,该如何解决?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

3条回答

  • zhangli00 csulizhang 3年前

    你这样找类名称是找不到的。要找到类的具体路径,从jni所在的那一层级开始往下目录查找,应该是这样找类,
    jclass objresults = env->FindClass("com/XXXX/XXXX/MyActivity/MyVersion");
    然后jfieldID my_majorversion = env->GetFieldID(objresults,"majorVersion","I");

    点赞 评论 复制链接分享
  • zhangli00 csulizhang 3年前

    最后那个MyVersion这个类是目录里面的成员类,我不确定是否是继续通过“/”来找到,我自己做,是在MyActivity这个类层级新建一个.java文件,声明MyVersion这个类,就百分百可以找到,你这样的做法,只能尝试一下com/XXXX/XXXX/MyActivity/MyVersion
    或com/XXXX/XXXX/MyActivity$MyVersion

    点赞 评论 复制链接分享
  • u014032613 xiefeng272 3年前

    java代码

     public class MyActivity extends Activity {
    
        public class MyVersion
        {
            public int majorVersion;
            public int minorVersion;
        }
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            // TODO Auto-generated method stub
            super.onCreate(savedInstanceState);
            int i =carlifelibstarttest();
            Toast.makeText(this, "Hello world", 10).show();
            System.out.println("Hello world!");
            TextView mTextview = new TextView(this);
            mTextview.setText(stringFromJNI());
            int j=connectionSetup();
            long p = class_init();
    
            carLifeLibInit();
            disconnect();
            carLifeLibDestory();
            MyVersion myVersion=new MyVersion();
            myVersion.majorVersion=1;
            myVersion.minorVersion=2;
            System.out.println(String.format("myVersion.majorVersion=%d,myVersion.minorVersion=%d", myVersion.majorVersion,myVersion.minorVersion));
            cmdHUProtoclVersion(myVersion);
            System.out.println(String.format("myVersion.majorVersion=%d,myVersion.minorVersion=%d", myVersion.majorVersion,myVersion.minorVersion));
            setContentView(mTextview);
    
        }
        public native int cmdHUProtoclVersion(MyVersion myVersion);
        public native int carlifelibstarttest();
        public native String stringFromJNI();
        public native long class_init();
        public native int connectionSetup();
        public native int carLifeLibInit();
        public native void carLifeLibDestory();
        public native void disconnect();
        static{
            System.loadLibrary("carlifeJni");
        }
    
    

    jni

     jint Java_com_xxxx_carlife_VehicleNative_cmdHUProtoclVersion(JNIEnv* env, jobject thiz,jobject myobject)
    {
        if(!g_obj)
            g_obj = env->NewGlobalRef(thiz);
        S_HU_PROTOCOL_VERSION *my_version=(S_HU_PROTOCOL_VERSION *)malloc(sizeof(S_HU_PROTOCOL_VERSION));
        jclass clazz;
        jfieldID my_majorversion;
        jfieldID my_minorversion;
    
        clazz= env->GetObjectClass(myobject);  
        if(clazz==NULL)
        {
            D("GetObjectClass Failed!\n");
            return -1;
        }
        my_majorversion=env->GetFieldID(clazz,"majorVersion","I");
        if(my_majorversion==NULL)
        {
            D("Failed to get majorVersion filedID!\n");
            return -1;
        }
        my_minorversion=env->GetFieldID(clazz,"minorVersion","I");
        if(my_minorversion==NULL)
        {
            D("Failed to get minorVersion filedID!\n");
            return -1;
        }
    
        my_version->majorVersion=env->GetIntField(clazz,my_majorversion);
        my_version->minorVersion=env->GetIntField(clazz,my_minorversion);
    
        D("majorVersion = %d,minorVersion = %d\n",my_version->majorVersion,my_version->minorVersion);
        env->SetIntField(clazz,my_majorversion,11);
        env->SetIntField(clazz,my_minorversion,22);
        D("majorVersion = %d,minorVersion = %d\n",my_version->majorVersion,my_version->minorVersion);
    
        return CCarLifeLib::getInstance()->cmdHUProtoclVersion(my_version);
        }
    

    签名

     {"cmdHUProtoclVersion","(Lcom/xxx/test/MyActivity$MyVersion;)I",(void*)Java_com_xxxx_carlife_VehicleNative_cmdHUProtoclVersion}
    
    点赞 评论 复制链接分享