Summary of problems in the pop-up prompt box when C ++ calls Android Functions

Source: Internet
Author: User

Summary of problems in the pop-up prompt box when C ++ calls Android Functions

(1) using global variables in Jni. cpp

 

JNIEnv *g_env;jobject g_object;

Then initialize JNIEnv and jobject In the first function:

 

 

JNIEXPORT jstring JNICALL Java_com_example_cocos2dinput_MainActivity_getStringFromC(JNIEnv* env,jobject thiz){g_env=env;g_object=thiz;return env->NewStringUTF("callCMessageBox");}

The following error occurs: The program crashes!

 

 

09-15 13:25:14.569: E/dalvikvm(15269): JNI ERROR (app bug): attempt to use stale local reference 0x1e800001

 

 

The reason is that in java jni, the object must be globally referenced for a global variable (I don't know if this is the case ..).

Use

 

g_object=(jobject)(env->NewGlobalRef(thiz));

Modify the code to the following OK:

 

 

JNIEXPORT jstring JNICALL Java_com_example_cocos2dinput_MainActivity_getStringFromC(JNIEnv* env,jobject thiz){g_env=env;g_object=(jobject)(env->NewGlobalRef(thiz));return env->NewStringUTF("callCMessageBox");}


 

 

 

(2) After modifying the problem above, the following problem occurs:

In C ++, a prompt box is displayed when I call Android. it is okay to not use global variables in cpp, but AndroidPlatform is used. the global variables g_object and g_env are used after cpp, so the above problem occurs. After problem 1 is fixed, the Android layer has a problem !!

 

Public static Context mContext; mContext = this. getApplicationContext (); public void showMessage () {Log. d ("showMessage", "showMessage"); AlertDialog. builder builder = new Builder (mContext); builder. setTitle ("C ++ calls Android"); builder. setMessage ("this is an example of C ++ calling Android"); builder. show ();}

The problem occurs below:

 

 

 android.view.WindowManager$BadTokenException: Unable to add window — token null 

This error is reported in the new AlertDialog. builder (mcontext), although the parameter here is AlertDialog. builder (Context context), but we cannot use the Context obtained by getApplicationContext (). Instead, we must use Activity, because only one Activity can add a form.

 

After the code is changed to the following, OK:

 

Public static Context mContext; mContext = MainActivity. this; public void showMessage () {Log. d ("showMessage", "showMessage"); AlertDialog. builder builder = new Builder (mContext); builder. setTitle ("C ++ calls Android"); builder. setMessage ("this is an example of C ++ calling Android"); builder. show ();}


 

Related Article

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.