Application的含义是在它的主线程中做了太多的工作



我正在制作一款简单的游戏。

第一个屏幕是闪屏,只有2个图像,1个进度条和一个背景图像。

第二个屏幕是选项选择从不同的关卡/游戏选项。它有几个文本字段,旋转项,2个图像和1个背景。这些图像和背景图像与开机画面相同。

从选项屏幕上有两种方式继续在游戏中的新活动要么选择"3x3"(MainActivity.class Activity)或4X4(bigmain.class Activity)。这两个活动都在屏幕上播放,包含一个相同的图像和一个网格布局,配有几个ImageViews。

MainActivity和bigmain都有onClick()网格布局方法。所有进一步的游戏处理,比如检查是否有人赢了,在不同玩家的回合中调用不同的方法。所以它们都有很多代码,都在一个java类文件中。是这个原因吗?如果是,那么如何克服它。当我通过adb在我的手机安装应用程序时,我正在附上我的logcat报告。

07-01 17:41:18.454 7003-7003/bt4u.com.pokemonbattles W/System:   ClassLoader referenced unknown path: /data/app/bt4u.com.pokemonbattles-2/lib/arm64
07-01 17:41:18.491 7003-7003/bt4u.com.pokemonbattles W/System: ClassLoader referenced unknown path: /data/app/bt4u.com.pokemonbattles-2/lib/arm64
07-01 17:41:18.492 7003-7003/bt4u.com.pokemonbattles I/LoadedApk: No resource references to update in package common
07-01 17:41:18.493 7003-7003/bt4u.com.pokemonbattles I/LoadedApk: No resource references to update in package com.rr.neptune
07-01 17:41:46.642 7332-7332/bt4u.com.pokemonbattles W/System: ClassLoader referenced unknown path: /data/app/bt4u.com.pokemonbattles-2/lib/arm64
07-01 17:41:47.290 7332-7332/bt4u.com.pokemonbattles W/System: ClassLoader referenced unknown path: /data/app/bt4u.com.pokemonbattles-2/lib/arm64
07-01 17:41:47.291 7332-7332/bt4u.com.pokemonbattles I/LoadedApk: No resource references to update in package common
07-01 17:41:47.292 7332-7332/bt4u.com.pokemonbattles I/LoadedApk: No resource references to update in package com.rr.neptune
**07-01 17:41:47.824 7332-7332/bt4u.com.pokemonbattles I/System.out: Splash screen activity (splash.java)**
07-01 17:41:47.834 7332-7363/bt4u.com.pokemonbattles D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true
07-01 17:41:47.863 7332-7363/bt4u.com.pokemonbattles I/Adreno: QUALCOMM build                   : 6d30364, I5d4b06969f
07-01 17:42:12.187 7660-7686/bt4u.com.pokemonbattles I/OpenGLRenderer: Initialized EGL, version 1.4
07-01 17:42:15.294 7660-7660/bt4u.com.pokemonbattles I/System.out: Game options activity (gameoptions.class)
07-01 17:42:15.350 7660-7660/bt4u.com.pokemonbattles D/android.widget.GridLayout: horizontal constraints: x1-x0>=243, x2-x1>=0, x3-x2>=0, x4-x3>=430, x4-x0<=656 are inconsistent; permanently removing: x4-x0<=656. 
**07-01 17:42:17.615 7660-7660/bt4u.com.pokemonbattles I/Choreographer: Skipped 125 frames!  The application may be doing too much work on its main thread.**
**07-01 17:48:20.373 7660-7660/bt4u.com.pokemonbattles I/System.out: Game Option A Screen (MainActivity.class)**
07-01 17:48:20.601 7660-7686/bt4u.com.pokemonbattles D/OpenGLRenderer: endAllStagingAnimators on 0x55c0c40f40 (RippleDrawable) with handle     0x55c08e70b0
**07-01 17:48:37.333 7660-7660/bt4u.com.pokemonbattles I/System.out: Game Option B Screen (bigmain.class)**
07-01 17:48:37.358 7660-7660/bt4u.com.pokemonbattles D/android.widget.GridLayout: vertical constraints: y4-y0>=900, y4-y3<=230, y3-y2<=230, y2-y1<=224, y1-y0<=210 are inconsistent; permanently removing: y4-y3<=230. 
07-01 17:48:37.685 7660-7686/bt4u.com.pokemonbattles D/OpenGLRenderer: endAllStagingAnimators on 0x55c0c40f40 (RippleDrawable) with handle 0x55c0d44e20

Application is doing too much work in main Thread表示您正在执行一些高任务,如large data object fetching和转换或loading with large Image,此时发生此错误。

更多的说明:可能是由于一些繁重的处理导致的在您的应用程序或数据库访问或任何其他的核心做什么导致线程停止一段时间的东西

解决方案:要解决这个问题,只需创建Multithreading

<标题>一致:
lets say you have for loop inside on Button so you have set it `OnClickListener` like this
  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_inventory);
    button.setOnclickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                   for(i=0;i<1000;i++){
                   --  Print i
                       i++;                   
                     }
                   }
            });
   }
像这样的

不要在click事件之后放置任何其他不可用的代码在里面,

这意味着你正在主UI线程上执行繁重的任务。这会导致ANR(应用程序不响应),因为您正在UI线程上运行一个进程,这需要很长时间。在此期间,GUI(图形用户界面)将被锁定,这将导致用户按下的任何东西都不会被操作。

你应该使用另一个线程来处理繁重的任务。

更好的方法是使用AsyncTask。

private class AsyncCaller extends AsyncTask<Void, Void, Void>
{
    ProgressDialog pdLoading = new ProgressDialog(AsyncExample.this);
    @Override
    protected void onPreExecute() {
        super.onPreExecute();
        //this method will be running on UI thread
        pdLoading.setMessage("tLoading...");
        pdLoading.show();
    }
    @Override
    protected Void doInBackground(Void... params) {
        //this method will be running on background thread so don't update UI frome here
        //do your long running http tasks here,you dont want to pass argument and u can access the parent class' variable url over here

        return null;
    }
    @Override
    protected void onPostExecute(Void result) {
        super.onPostExecute(result);
        //this method will be running on UI thread
        pdLoading.dismiss();
    }
    }

最新更新