"Android" Locates and resolves the ANR error record

Source: Internet
Author: User


Problem description


Cocos2d-x Game Project Android Project Access SDK, after the successful payment, Java code callback Lua method, generated the ANR.


How to position ANR?



Records and ANR error messages in the Data/anr/traces.txt file. You can use the RE manager to view the file.


In the log information, you can see how the Cocos2d-x audio engine plays the sound effect called in the callback method. This is where the ANR is produced. I'm using cocos2d-x2.1.5 here.

After finding the source of the problem, I commented on the code that played the sound. And then run again, there is no ANR generated.


However, another problem arises ...


An OpenGL error was reported, and the creation of the Ccsprite failed. This problem typically occurs because Ccspirte is not created in the GL thread.


Main thread vs. GL Threads


Main thread (UI thread): Threads created when the app starts, other threads are child threads of the thread, and are primarily used to update the UI's threads.

GL Thread: A child thread of the main thread that is used primarily to update the GUI thread. In Cocos2d-x, a GL thread is separated from the main thread for work related to screen rendering (to ensure smooth graphics).


Under Android, activity has a runonuithread method, which is used to perform a task in the main thread. Note that if the task is time-consuming it will produce ANR.

The declaration of the method is as follows:

public void Runonuithread (Runnable task);

OpenGL's rendering under Android needs to deal with Glsurfaceview. So the cocos2d-x encapsulates a cocos2dxglsurfaceview. The view is related to the GL thread. Cocos2dxactivity contains a cocos2dxglsurfaceview and provides a Runonglthread method for performing a task in a GL thread.

The method declaration is as follows:

public void Runonglthread (final Runnable Task);

Implementation on the Glsurfaceview is called by the Queueevent method to implement communication with the GL thread. The implementation mechanism is also a message poll under Android.



Finally, by using the Runonglthread method to callback the Lua method in the GL thread, the problem is solved completely.

Ctx.runonglthread (New Runnable () {@Overridepublic void run () {paytools.paycallback ();//lua method needs to be called in GL Thread}});

"Android" Locates and resolves the ANR error record

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.