Ndk Development notes (III)-JNI Learning

Source: Internet
Author: User

1. This article mainly describes the use of the getfieldid and getmethodid methods.


 

 

Jclass clazz = (* env)-> getobjectclass (ENV, thiz); // clazz is the natvie class.

 

Public class native {<br/> Public string value = "hello "; <br/>/** <br/> * This will return from JNI <br/> * @ return <br/> */<br/> Public static int number = 0; <br/> Public static native string stringfromjni (); <br/>/** <br/> * pass the message to the JNI and <br/> * @ Param Meg <br/> * @ return JNI deal it message <br /> */<br/> Public static native string getmessage (string MEG ); <br/> Public native void getvalue (); </P> <p> Public void showmessage () {<br/> system. out. println ("Call the show message"); <br/>}< br/>} 

 

 

At first, when I was writing this program, I always reported getfileid... no such "value ".

After searching for a long time, I found that the getvalue () defined in native. Java also uses static

 

The defined value is not static.

An error is reported in FID = (* env)-> getfieldid (ENV, clazz, "value", "ljava/lang/string;"); because the static variable must be used in the static method.

So I removed the static method of the getvalue () method. below is the OK code.

Void Merge (jnienv * ENV, jobject thiz) {<br/> jfieldid FID; <br/> jfieldid fnumberid; <br/> jstring jstr; <br/> jmethodid mid; <br/> const char * sztemp; <br/> jclass clazz = (* env)-> getobjectclass (ENV, thiz); <br/> Print (clazz ); <br/> FID = (* env)-> getfieldid (ENV, clazz, "value", "ljava/lang/string ;"); <br/> If (FID = NULL) {<br/> return; <br/>}< br/> jstr = (* env)-> getobjectfield (ENV, thiz, FID); <br/> sztemp = (* env)-> getstringutfchars (ENV, jstr, null); <br/> Print (sztemp ); <br/> (* env)-> releasestringutfchars (ENV, jstr, sztemp); <br/> jstr = (* env)-> newstringutf (ENV, "Hello World"); <br/> If (jstr = NULL) <br/> return; <br/> (* env)-> setobjectfield (ENV, thiz, FID, jstr); <br/> // call the method showmessage <br/> fnumberid = (* env)-> getstaticfieldid (ENV, clazz, "Number ", "I"); <br/> If (fnumberid = NULL) <br/> return; <br/> jint num = (* env)-> getstaticintfield (ENV, clazz, fnumberid); <br/> // print (Num); <br/> (* env)-> setstaticintfield (ENV, clazz, fnumberid, 2 ); <br/> mid = (* env)-> getmethodid (ENV, clazz, "showmessage", "() V"); <br/> If (mid = NULL) <br/> return; <br/> (* env)-> callvoidmethod (ENV, thiz, mid); <br/>} 

 

 

I used static to solve all the problems.

FID = (* env)-> getstaticfieldid (ENV, clazz, "value", "ljava/lang/string ;");

 

I don't know why, or the above error occurs .. Later.

 

 

 

Try getmethodid () today ();

 

Mid = (* env)-> getmethodid (ENV, clazz, "showmessage", "() V"); // showmessage is a method in Java ,"() V "indicates that this method is the return void method.

(* Env)-> callvoidmethod (ENV, thiz, mid );

 

 

Corresponding to getmethodid is call ** method ().

 

The static attribute must be used.

 

 

Fnumberid = (* env)-> getstaticfieldid (ENV, clazz, "Number", "I ");

If (fnumberid = NULL)

Return;

Jint num = (* env)-> getstaticintfield (ENV, clazz, fnumberid );

// Print (Num );

(* Env)-> setstaticintfield (ENV, clazz, fnumberid, 2 );

 

 

 

If you need source code, please leave a message to give the mail address.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.