我是Android SDK/API环境的新手。这是我第一次试着画一个图表。我尝试在模拟器上使用3个不同的免费库运行不同种类的示例代码,没有显示在布局屏幕上。日志猫正在重复如下信息:

 W/Trace(1378): Unexpected value from nativeGetEnabledTags: 0
 I/Choreographer(1378): Skipped 55 frames!  The application may be doing too much work on its main thread. 

当我运行一个与授权库的评估副本相关的示例代码时,这个问题并没有持续存在,图表也正常工作。


当前回答

Android UI:修复跳过的帧

任何开始开发android应用程序的人都会看到这条消息 “编舞(abc):跳过xx帧!申请可以是 在主线上做了太多工作。”那么它实际上是什么 意思是,你为什么要担心以及如何解决它。

这意味着您的代码需要很长时间来处理和帧 都因为它而被跳过,这可能是因为一些沉重的 您在应用程序或数据库的核心所做的处理 访问或任何其他事情,导致线程停止一段时间。

Here is a more detailed explanation: Choreographer lets apps to connect themselves to the vsync, and properly time things to improve performance. Android view animations internally uses Choreographer for the same purpose: to properly time the animations and possibly improve performance. Since Choreographer is told about every vsync events, I can tell if one of the Runnables passed along by the Choreographer.post* apis doesnt finish in one frame’s time, causing frames to be skipped. In my understanding Choreographer can only detect the frame skipping. It has no way of telling why this happens. The message “The application may be doing too much work on its main thread.” could be misleading. source : Meaning of Choreographer messages in Logcat Why you should be concerned When this message pops up on android emulator and the number of frames skipped are fairly small (<100) then you can take a safe bet of the emulator being slow – which happens almost all the times. But if the number of frames skipped and large and in the order of 300+ then there can be some serious trouble with your code. Android devices come in a vast array of hardware unlike ios and windows devices. The RAM and CPU varies and if you want a reasonable performance and user experience on all the devices then you need to fix this thing. When frames are skipped the UI is slow and laggy, which is not a desirable user experience. How to fix it Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying “Only the original thread that created a view hierarchy can touch its views“. You need to know this fact that UI in android can be changed by the main thread or the UI thread only. Any other thread which attempts to do so, fails and crashes with this error. What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. Find an example here. So we have Thread and Runnable for processing data out of main Thread, what else? There is AsyncTask in android which enables doing long time processes on the UI thread. This is the most useful when you applications are data driven or web api driven or use complex UI’s like those build using Canvas. The power of AsyncTask is that is allows doing things in background and once you are done doing the processing, you can simply do the required actions on UI without causing any lagging effect. This is possible because the AsyncTask derives itself from Activity’s UI thread – all the operations you do on UI via AsyncTask are done is a different thread from the main UI thread, No hindrance to user interaction. So this is what you need to know for making smooth android applications and as far I know every beginner gets this message on his console.

其他回答

尝试使用以下策略来提高应用程序的性能:

尽可能使用多线程编程。即使你的智能手机只有一个核(如果处理器有两个或更多核,线程可以在不同的核上运行),性能上的好处也是巨大的。让你的应用逻辑与UI分离是很有用的。使用Java线程,AsyncTask或IntentService。检查这个。 阅读并遵循Android开发网站的各种性能提示。检查在这里。

UI线程延迟的另一个常见原因是SharedPreferences访问。当你调用PreferenceManager。getSharedPreferences和其他类似的方法,关联的.xml文件将立即在同一个线程中加载和解析。

解决这个问题的一个好方法是从后台线程触发第一次SharedPreference加载,越早越好(例如从你的Application类的onCreate开始)。这样,在您想要使用首选项对象时,首选项对象可能已经构造好了。

Unfortunately, sometimes reading a preference files is necessary during early phases of startup (e.g. in the initial Activity or even Application itself). In such cases it is still possible to avoid stalling UI by using MessageQueue.IdleHandler. Do everything else you need to perform on the main thread, then install the IdleHandler to execute code once your Activity have been fully drawn. In that Runnable you should be able to access SharedPreferences without delaying too many drawing operations and making Choreographer unhappy.

在我的例子中,这是因为我不小心在一个方法上设置了一个断点。一旦我清除了它,消息就消失了,性能提高了很多。

Android UI:修复跳过的帧

任何开始开发android应用程序的人都会看到这条消息 “编舞(abc):跳过xx帧!申请可以是 在主线上做了太多工作。”那么它实际上是什么 意思是,你为什么要担心以及如何解决它。

这意味着您的代码需要很长时间来处理和帧 都因为它而被跳过,这可能是因为一些沉重的 您在应用程序或数据库的核心所做的处理 访问或任何其他事情,导致线程停止一段时间。

Here is a more detailed explanation: Choreographer lets apps to connect themselves to the vsync, and properly time things to improve performance. Android view animations internally uses Choreographer for the same purpose: to properly time the animations and possibly improve performance. Since Choreographer is told about every vsync events, I can tell if one of the Runnables passed along by the Choreographer.post* apis doesnt finish in one frame’s time, causing frames to be skipped. In my understanding Choreographer can only detect the frame skipping. It has no way of telling why this happens. The message “The application may be doing too much work on its main thread.” could be misleading. source : Meaning of Choreographer messages in Logcat Why you should be concerned When this message pops up on android emulator and the number of frames skipped are fairly small (<100) then you can take a safe bet of the emulator being slow – which happens almost all the times. But if the number of frames skipped and large and in the order of 300+ then there can be some serious trouble with your code. Android devices come in a vast array of hardware unlike ios and windows devices. The RAM and CPU varies and if you want a reasonable performance and user experience on all the devices then you need to fix this thing. When frames are skipped the UI is slow and laggy, which is not a desirable user experience. How to fix it Fixing this requires identifying nodes where there is or possibly can happen long duration of processing. The best way is to do all the processing no matter how small or big in a thread separate from main UI thread. So be it accessing data form SQLite Database or doing some hardcore maths or simply sorting an array – Do it in a different thread Now there is a catch here, You will create a new Thread for doing these operations and when you run your application, it will crash saying “Only the original thread that created a view hierarchy can touch its views“. You need to know this fact that UI in android can be changed by the main thread or the UI thread only. Any other thread which attempts to do so, fails and crashes with this error. What you need to do is create a new Runnable inside runOnUiThread and inside this runnable you should do all the operations involving the UI. Find an example here. So we have Thread and Runnable for processing data out of main Thread, what else? There is AsyncTask in android which enables doing long time processes on the UI thread. This is the most useful when you applications are data driven or web api driven or use complex UI’s like those build using Canvas. The power of AsyncTask is that is allows doing things in background and once you are done doing the processing, you can simply do the required actions on UI without causing any lagging effect. This is possible because the AsyncTask derives itself from Activity’s UI thread – all the operations you do on UI via AsyncTask are done is a different thread from the main UI thread, No hindrance to user interaction. So this is what you need to know for making smooth android applications and as far I know every beginner gets this message on his console.

我不是专家,但当我想从我的android应用程序发送数据到web服务器时,我得到了这个调试消息。虽然我使用AsyncTask类并在后台进行数据传输,但为了从服务器获取结果数据,我使用了AsyncTask类的get()方法,这使得UI同步,这意味着你的UI将等待太长时间。所以我的建议是让你的应用程序在一个单独的线程上执行每个面向网络的任务。