gpt4 book ai didi

android - 主线程中的工作太多,应用程序卡住

转载 作者:塔克拉玛干 更新时间:2023-11-02 22:05:49 24 4
gpt4 key购买 nike

在这里,我调用了一个 Activity ,它是一个聊天应用程序。

猫日志:

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.
. . . . .
. . . . .
. . . . .

这就是我从 MainActivity 启动 Activity 的方式:

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);
}
});

我怎样才能防止这种情况,应用程序在调用 Activity 后挂起。有什么问题吗?

更新:

飞溅 Activity :

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) {
}
}

更新:

您可以在 this 中找到详细信息线程。

最佳答案

startActivity(i); 只需向系统发送一个 Intent。所以你不需要在单独的线程中启动 Activity 。

相反,您应该在另一个线程中通过 SplashActivity onCreate 方法完成所有艰苦的工作。

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();
}
}

请记住,您只能在主 Android(通常称为 UI)线程上更新您的 UI。

我建议您的 QBAuth.createSession(this); 正在打开某种 Http 连接,这就是您的应用程序卡住的原因。将其移至单独的线程。

更新:

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();
}
}
});
}
}

关于android - 主线程中的工作太多,应用程序卡住,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22049375/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com