Android 应用程序显示黑屏?
我正在实现一个android应用程序。我正在一个活动中使用web服务。我将显示一个进度对话框,直到它加载第二个活动。但它不会一直显示,而且会有一段时间显示黑屏,看起来像是应用程序挂起。我该怎么办?我浪费了三天的时间。我正在为这些进程使用asynctask。请帮帮我Android 应用程序显示黑屏?,android,android-asynctask,Android,Android Asynctask,我正在实现一个android应用程序。我正在一个活动中使用web服务。我将显示一个进度对话框,直到它加载第二个活动。但它不会一直显示,而且会有一段时间显示黑屏,看起来像是应用程序挂起。我该怎么办?我浪费了三天的时间。我正在为这些进程使用asynctask。请帮帮我 protected void onListItemClick(ListView l, View v, final int position, long id) { super.onListItemClick(l
protected void onListItemClick(ListView l, View v, final int position,
long id) {
super.onListItemClick(l, v, position, id);
progressDialog = ProgressDialog.show(ProjectListActivity.this,
"Please wait...", "Loading...");
new Thread() {
public void run() {
try {
String project = titles.get(position - 1);
performBackgroundProcess(project);
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
progressDialog.dismiss();
}
}.start();
}
private void performBackgroundProcess(String project) {
String spaceId = null;
String spaceName = null;
/*
* for (Space space : spaces){
* if(space.getName().equalsIgnoreCase((String) ((TextView)
* v).getText())){ spaceId = space.getId(); } }
*/
for (Space space : spaces) {
if (project.equals(space.getName())) {
newSpace = space;
}
}
spaceId = newSpace.getId();
spaceName = newSpace.getName();
/*
* Intent intent = new Intent(this, SpaceComponentsActivity.class);
* intent.putExtra("spaceId", spaceId); intent.putExtra("tabId", 0);
* intent.putExtra("className", "TicketListActivity"); TabSettings ts =
* new TabSettings(); ts.setSelTab(1); this.startActivity(intent);
*/
Intent intent = new Intent(this, SpaceComponentsActivity.class);
intent.putExtra("spaceId", spaceId);
intent.putExtra("tabId", 0);
intent.putExtra("spaceName", spaceName);
// intent.putExtra("className", "TicketListActivity");
TabSettings ts = new TabSettings();
ts.setSelTab(0);
ts.setSelTabClass("TicketListActivity");
this.startActivity(intent);
/*
* Toast.makeText(getApplicationContext(), ((TextView) v).getText(),
* Toast.LENGTH_SHORT).show();
*/
}
对于您的情况,请使用此 将
progressDialog
公开给您的活动
progressDialog = ProgressDialog.show(ProjectListActivity.this,
"Please wait...", "Loading...");
new Thread() {
public void run() {
try {
String project = titles.get(position - 1);
performBackgroundProcess(project);
ProjectListActivity.this.runOnUiThread(new Runnable() {
@Override
public void run() {
progressDialog.dismiss();
}
});
} catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
}.start();
但这并不是一个好的使用方法。私有类ProgressTask扩展了AsyncTask{
private ProgressDialog=新建ProgressDialog(HomeActivity.this);
受保护的void onPreExecute(){
this.dialog.setMessage(“请稍候”);
this.dialog.show();
}
受保护的布尔doInBackground(最终字符串…args){
试一试{
Utilities.arrayRSS=objRSSFeed
.FetchRSSFeeds(Constants.Feed\u URL);
返回true;
}捕获(例外e){
Log.e(“标记”、“错误”,e);
返回false;
}
}
@凌驾
受保护的void onPostExecute(最终布尔值成功){
if(dialog.isShowing()){
dialog.dismise();
}
如果(成功){
//显示用户界面
setText(Utilities.rstitle);
}
}
我就是这样实现的。在这里为其他人复制代码。希望它也能为您工作。您可以复制此函数“display\u splash\u screen()”并从OnCreate()函数调用它(或在任何需要它的地方)
我的对话框(启动屏幕)R.layout.welcome_启动屏幕将显示,然后在一个线程中,我在3秒钟后关闭对话框
`
`您的代码在哪里?盲目猜测您正在使用主线程调用
webService
。用于调用wed service
。我正在实现performBackgroundProcess方法中的所有逻辑。还需要发布吗?正如M Mohsin Naeem所说。不要使用线程,使用AsyncTask。我还添加了performBackgroundProcess方法的代码,请G现在我想知道如何实现asynctask。感谢M Mohsin先生的代码,但它显示出与前一个相同的问题。我还发布了performbackground流程的代码。请建议如何为它实现asynctask。在performBackgroundProcess()中,什么是耗时的流程
?我想你刚刚启动了一个活动
?为AsyncTask
调用Web服务的在哪里?你需要自己阅读:)如果你遇到任何问题,然后再来问..非常欢迎..让我们来吧,因为这是从Web服务获取数据。在这个过程中需要花费时间。直到有什么事情需要我交互式图形用户界面。
private class ProgressTask extends AsyncTask<String, Void, Boolean> {
private ProgressDialog dialog = new ProgressDialog(HomeActivity.this);
protected void onPreExecute() {
this.dialog.setMessage("Please wait");
this.dialog.show();
}
protected Boolean doInBackground(final String... args) {
try {
Utilities.arrayRSS = objRSSFeed
.FetchRSSFeeds(Constants.Feed_URL);
return true;
} catch (Exception e) {
Log.e("tag", "error", e);
return false;
}
}
@Override
protected void onPostExecute(final Boolean success) {
if (dialog.isShowing()) {
dialog.dismiss();
}
if (success) {
// display UI
txtTitle.setText(Utilities.RSSTitle);
}
}
private void display_splash_screen() {
try{
// custom dialog
final Dialog dialog = new Dialog(MainActivity.this);
dialog.setContentView(R.layout.welcome_splash_screen);
dialog.setTitle("Bulk SMS");
dialog.setCancelable(true);
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
@Override
public void onShow(DialogInterface dialogInterface) {
Thread thrDialogClose = new Thread(){
@Override
public void run() {
super.run();
try {
// Let the dialog be displayed for 3 secs
sleep(3000);
} catch (InterruptedException e) {
e.printStackTrace();
}
dialog.dismiss();
}
};
thrDialogClose.start();
}});
dialog.setCanceledOnTouchOutside(true);
dialog.show();
}catch (Exception ex){
Log.e("Bulk SMS:", ex.getStackTrace().toString());
}
}