Too much work in main thread, app freezes

匿名 (未验证) 提交于 2019-12-03 01:13:01

问题:

Here I make call to an activity, which is a chat application.

The catLog:

02-26 12:30:38.996: I/Choreographer(807): Skipped 35 frames!  The application may be doing too much work on its main thread. 02-26 12:30:39.196: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread. 02-26 12:30:39.516: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread. 02-26 12:30:39.996: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread. 02-26 12:30:40.066: I/Choreographer(807): Skipped 37 frames!  The application may be doing too much work on its main thread. 02-26 12:30:40.207: I/Choreographer(807): Skipped 33 frames!  The application may be doing too much work on its main thread. 02-26 12:30:40.896: I/Choreographer(807): Skipped 33 frames!  The application may be doing too much work on its main thread. 02-26 12:30:41.586: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread. 02-26 12:30:42.266: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread. 02-26 12:30:42.486: I/Choreographer(807): Skipped 31 frames!  The application may be doing too much work on its main thread. 02-26 12:30:42.556: I/Choreographer(807): Skipped 37 frames!  The application may be doing too much work on its main thread. 02-26 12:30:42.826: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread. 02-26 12:30:43.316: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread. 02-26 12:30:43.777: I/Choreographer(807): Skipped 30 frames!  The application may be doing too much work on its main thread. 02-26 12:30:43.826: I/Choreographer(807): Skipped 32 frames!  The application may be doing too much work on its main thread. 02-26 12:30:43.866: I/Choreographer(807): Skipped 34 frames!  The application may be doing too much work on its main thread. .   .      .   .      .   .   .      .   .      . .   .      .   .      . 

This is how I start the activity from MainActivity:

public class MainActivity extends Activity {      Handler handler; @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_main);      Button b = (Button) findViewById(R.id.button1);      handler = new Handler(Looper.getMainLooper());       b.setOnClickListener(new View.OnClickListener() {         public void onClick(View v) {             final  Intent i = new Intent(com.example.mainactivity.MainActivity.this,com.quickblox.sample.chat.ui.activities.SplashActivity.class);              handler.post(new Runnable() {                     @Override                     public void run() {                         startActivity(i);                     }                 }); 

How can I prevent this,the app hangs after calling the activity.Whats wrong?

UPDATE:

SplashActivity:

public class SplashActivity extends Activity implements QBCallback {      private static final String APP_ID = "7467";     private static final String AUTH_KEY = "TxRFWfX8tTXQ4gv";     private static final String AUTH_SECRET = "y-QJrO2j69VTaCs";      private ProgressBar progressBar;      @Override     protected void onCreate(Bundle savedInstanceState) {         super.onCreate(savedInstanceState);         setContentView(R.layout.activity_splash);          progressBar = (ProgressBar) findViewById(R.id.progressBar);          QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY, AUTH_SECRET);         QBAuth.createSession(this);     }      @Override     public void onComplete(Result result) {         progressBar.setVisibility(View.GONE);          if (result.isSuccess()) {             Intent intent = new Intent(this, MainActivity.class);             startActivity(intent);             finish();         } else {             AlertDialog.Builder dialog = new AlertDialog.Builder(this);             dialog.setMessage("Error(s) occurred. Look into DDMS log for details, " +                     "please. Errors: " + result.getErrors()).create().show();         }     }      @Override     public void onComplete(Result result, Object context) {     } } 

UPDATE :

You can find Details in this thread.

回答1:

First of all, the handler in the main activity is completely useless. The onClick callback is called by the system on the UI thread, and your handler is also bound to the UI thread, so you can just go without it. But i doubt you are getting the problems there, it just a small overhead.

You may want to try to move the calls

QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY, AUTH_SECRET); QBAuth.createSession(this); 

to a background thread, but they may expect to be called on the UI thread, which will cause some problems.

As your code is 1:1 with the code here: https://github.com/QuickBlox/quickblox-android-sdk/tree/master/sample-chat , I would first try the sample app to check if the problem can also to be seen there. If yes, just open an issue in the github projet :)

If no, then you are doing something differently, and we will need more code to investigate...



回答2:

startActivity(i); just send an Intent to the system. So you don't need to start activity in seperate thread.

Instead you should do all your hard work fromSplashActivity onCreate method in another thread.

public class MainActivity extends Activity { //...  @Override  protected void onCreate(Bundle savedInstanceState) {    //...    final  Intent intent = new Intent(this,SplashActivity.class);    startActivity(intent);  } }  public class SplashActivity extends Activity { //...  @Override  protected void onCreate(Bundle savedInstanceState) {    //...    new Thread(new Runnable()    {      @Override      public void run() {       //do loading data or whatever hard here        runOnUiThread(new Runnable(){         @Override         public void run() {           //..update your UI here         }       });      }    }).start();  } } 

Remeber that you can update your UI only on Main Android(often called as UI) thread.

I suggest that yours QBAuth.createSession(this); are opening some sort of Http connection, that why your App gets freeze. Move it to separate thread.

UPDATE :

public class SplashActivity extends Activity{  private static final String APP_ID = "7467"; private static final String AUTH_KEY = "TxRFWfX8tTXQ4gv"; private static final String AUTH_SECRET = "y-QJrO2j69VTaCs";  public static class QBAuth{     private static QBCallback mQBCallback;      public static void createSession(QBCallback qbCallback) {         // do request initialization         mQBCallback = qbCallback;     } }  public interface QBCallback{     public void onComplete(Result result);     public void onComplete(Result result, Object context); }  public class Result{     boolean isSuccess;     String errors;      public boolean isSuccess() {         return isSuccess;     }      public void setSuccess(boolean isSuccess) {         this.isSuccess = isSuccess;     }      public String getErrors() {         return errors;     }      public void setErrors(String errors) {         this.errors = errors;     } }  private ProgressBar progressBar;  @Override protected void onCreate(Bundle savedInstanceState) {     super.onCreate(savedInstanceState);     setContentView(R.layout.activity_splash);      progressBar = (ProgressBar) findViewById(R.id.progressBar);       new Thread(new Runnable() {         @Override         public void run() {             QBSettings.getInstance().fastConfigInit(APP_ID, AUTH_KEY, AUTH_SECRET);             QBAuth.createSession(mQBCallback);         }     }); }   private QBCallback mQBCallback = new QBCallback() {     @Override     public void onComplete(Result result) {         handleResult(result);     }      @Override     public void onComplete(Result result, Object context) {      } };  private void handleResult(final Result result) {     runOnUiThread(new Runnable() {         @Override         public void run() {             progressBar.setVisibility(View.GONE);              if (result.isSuccess()) {                 Intent intent = new Intent(SplashActivity.this, MainActivity.class);                 startActivity(intent);                 finish();             } else {                 AlertDialog.Builder dialog = new AlertDialog.Builder(SplashActivity.this);                 dialog.setMessage("Error(s) occurred. Look into DDMS log for details, " +                         "please. Errors: " + result.getErrors()).create().show();             }         }     }); } } 


回答3:

Try changing :

android:label="@string/app_name" 

cut from your called activity, and paste in your launcher activity.



易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!