2012-07-18 54 views
1

我正在实现一个android应用程序。我正在一项活动中使用Web服务。我显示一个进度对话框,直到它加载第二个活动。但它并没有显示整个时间,并显示一段时间的黑屏。它看起来像应用程序挂起。我该怎么办?我浪费了我三天的时间。我正在使用asynctask进行这些过程。请帮帮我 。应用程序显示黑屏?

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(); 
    */ 
} 
+0

哪里是你的代码?盲猜你是使用主线程来调用'webService'.use http://developer.android.com/reference/android/os/AsyncTask.html来调用'wed服务'。 – 2012-07-18 06:01:20

+0

我张贴我的代码。 – 2012-07-18 06:03:34

+0

我正在执行performBackgroundProcess方法中的所有逻辑。需要发布它呢?正如M Mohsin Naeem所说的 – 2012-07-18 06:10:23

回答

2

为你的情况使用此...

做出progressDialog公众对你的Activity

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

,但它是不是一个好的计算策略的使用AsyncTask

+0

感谢M Mohsin先生为您的代码,但它显示与上一个问题相同的问题。我还发布了performbackground流程的代码。请建议如何执行asynctask任务。 – 2012-07-18 06:23:19

+0

'performBackgroundProcess()'中的时间处理过程是什么?我想你只是推出一个'活动'? “AsyncTask”的“Web服务调用”在哪里,你需要自己阅读:)如果你面临任何问题,然后再次问...你最欢迎.. – 2012-07-18 06:30:23

+0

是的,我正在启动后台进程中的活动。 – 2012-07-18 06:49:42

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

这就是我实现它的方式。将代码复制到其他人。希望它也适用于你。你可以复制这个函数“display_splash_screen()”并从你的OnCreate()函数中调用它(或者无论它是否需要)。

我的对话框(启动画面)显示R.layout.welcome_splash_screen,然后在一个线程中3秒后关闭对话框。

`

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

`