1
回答
android jni jobjectArray
利用AWS快速构建适用于生产的无服务器应用程序,免费试用12个月>>>   
public void testJNI(){
        JTest mJTest = new JTest();
        MailInfo[] mMailInfos = mJTest.mObjMethod("Thi");
        if (mMailInfos.length == 0) {
            System.out.println(">>>>>>>>>>>>>>>>mMailInfos.length");
            return ;
        }
        for (int i = 0; i < mMailInfos.length; i++) {
            System.out.println(mMailInfos .topic);
        }
    }
//////////////////////////////////////////////////////////////////////
public class JTest {
    public native MailInfo[] mObjMethod(String text);
}

////////////////////////////////////////////////////////////////////////
public class MailInfo {
    public String topic = "xxx";
}

///////////////////////////////////////////////////////////////////////
/*
* Class:     com_inspur_jni_declare_JTest
* Method:    mObjMethod
* Signature: (Ljava/lang/String;)[Lcom/inspur/comm/MailInfo;
*/
JNIEXPORT jobjectArray JNICALL Java_com_inspur_jni_declare_JTest_mObjMethod (JNIEnv * env, jobject obj, jstring str){

    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    jobjectArray mailInfos = 0;
    jsize len = 5;
    jclass obj_clazz = (*env)->FindClass(env, "java/lang/Object");  //获取object所属类,一般为ava/lang/Object就可以了
    mailInfos = (*env)->NewObjectArray(env, len, obj_clazz, 0);//新建object数组

    jclass jtest_clazz = (*env)->FindClass(env, "com/inspur/jni/declare/JTest");
    jclass mailInfo_clazz = (*env)->FindClass(env, "com/inspur/comm/MailInfo"); //获取Java中的实例类
    if(mailInfo_clazz == NULL){
        LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    }
    jmethodID mailInfo_methodid = (*env)->GetMethodID(env, mailInfo_clazz, "<init>", "()V");
    jobject mailInfo_obj = (*env)->NewObject(env, mailInfo_clazz, mailInfo_methodid);
    jfieldID topic_id = (*env)->GetFieldID(env, mailInfo_clazz, "topic", "Ljava/lang/String;");
    if(topic_id == NULL){
        LOGE(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    }
    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);

    jstring jstr = (*env)->GetObjectField(env, mailInfo_obj, topic_id);
    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    if(jstr == NULL){
        LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    }
    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    (*env)->SetObjectField(env, mailInfo_obj, topic_id, (*env)->NewStringUTF(env, "miyoyo"));
    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    (*env)->SetObjectArrayElement(env, mailInfos, 0, mailInfo_obj);
    LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);

    if(mailInfos == NULL){
        LOGV(">>>>>>%s,%d",__FUNCTION__,__LINE__);
    }

    return mailInfos;
}
///////////////////////////////////////////////////////////////////////
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,49
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,66
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,69
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,73
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,75
08-17 02:51:13.896: DEBUG/JNITest(657): >>>>>>Java_com_inspur_jni_declare_JTest_mObjMethod,77
08-17 02:51:13.906: WARN/dalvikvm(657): JNI WARNING: method declared to return '[Lcom/inspur/comm/MailInfo;' returned '[Ljava/lang/Object;'
08-17 02:51:13.906: WARN/dalvikvm(657):              failed in Lcom/inspur/jni/declare/JTest;.mObjMethod
08-17 02:51:13.906: INFO/dalvikvm(657): "main" prio=5 tid=1 RUNNABLE
08-17 02:51:13.906: INFO/dalvikvm(657):   | group="main" sCount=0 dsCount=0 s=N obj=0x4001d8e0 self=0xccb0
08-17 02:51:13.906: INFO/dalvikvm(657):   | sysTid=657 nice=0 sched=0/0 cgrp=default handle=-1345026008
08-17 02:51:13.906: INFO/dalvikvm(657):   | schedstat=( 119231523 446388043 66 )
08-17 02:51:13.906: INFO/dalvikvm(657):   at com.inspur.jni.declare.JTest.mObjMethod(Native Method)
08-17 02:51:13.906: INFO/dalvikvm(657):   at com.inspur.webjs.WebJsTestActivity.testJNI(WebJsTestActivity.java:81)



为什么红色部分只要一打开就挂啦!!!为什么呢???
举报
miyuehu
发帖于5年前 1回/1K+阅
顶部