2013-03-07 70 views
0

我是android中的新手,我想在android中從一個活動移動到其他活動時顯示進度,這裏是code.Exception是「Activity com.example.mytest.ReadContactsActivity has leaked window com .android.internal.policy.impl.PhoneWindow $ {DecorView VE 40ce4900 .... [R .....我。0,0-295,62},最初這裏加入」如何在android中顯示進度

class MyTask extends AsyncTask<Void, Integer, Void> { 

Dialog dialog; 
ProgressBar progressBar; 
TextView tvLoading,tvPer; 


@Override 
protected void onPreExecute() { 
    super.onPreExecute(); 
    dialog = new Dialog(ReadContactsActivity.this); 
    dialog.setCancelable(false); 
    dialog.requestWindowFeature(Window.FEATURE_NO_TITLE); 
    dialog.setContentView(R.layout.progressdialog); 

    progressBar = (ProgressBar) dialog.findViewById(R.id.progressBar1); 
    tvLoading = (TextView) dialog.findViewById(R.id.tv1); 
    dialog.show(); 
} 

@Override 
protected Void doInBackground(Void... params) { 

textViewDisplay = (TextView) findViewById(R.id.textViewDisplay); 
ContentResolver cr = getContentResolver(); 
Cursor cur = cr.query(ContactsContract.Contacts.CONTENT_URI, 
     null, null, null, null); 

if (cur.getCount() > 0) { 
    while (cur.moveToNext()) { 
     String id = cur.getString(cur.getColumnIndex(ContactsContract.Contacts._ID)); 
     String name = cur.getString(cur.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)); 
     if (Integer.parseInt(cur.getString(cur.getColumnIndex(ContactsContract.Contacts.HAS_PHONE_NUMBER))) > 0) { 
      System.out.println("name : " + name + ", ID : " + id); 
      textViewDisplay.append("Name: "); 
      textViewDisplay.append(name); 

      // get the phone number 
      Cursor pCur = cr.query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI,null, 
            ContactsContract.CommonDataKinds.Phone.CONTACT_ID +" = ?", 
            new String[]{id}, null); 
      while (pCur.moveToNext()) { 
        String phone = pCur.getString(
         pCur.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER)); 
        textViewDisplay.append(",Number: "+ phone); 
        textViewDisplay.append("\n");  
      } 
      pCur.close(); 
      } 
     } 
    } 
return null; 
} 

@Override 
protected void onProgressUpdate(Integer... values) { 
    super.onProgressUpdate(values); 
    progressBar.setProgress(values[0]); 
    tvLoading.setText("Loading... " + values[0] + " %"); 
    tvPer.setText(values[0]+" %"); 
} 

@SuppressWarnings("deprecation") 
@Override 
protected void onPostExecute(Void result) { 
    super.onPostExecute(result); 

    dialog.dismiss(); 

    AlertDialog alert = new AlertDialog.Builder(ReadContactsActivity.this) 
      .create(); 

    alert.setTitle("Completed!!!"); 
    alert.setMessage("Your Task is Completed SuccessFully!!!"); 
    alert.setButton("Dismiss", new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
      dialog.dismiss(); 

     } 
    }); 
    alert.show(); 
} 
} 
+0

它是一個內部類或單獨類 – Pragnani 2013-03-07 17:54:37

+0

這是內部類 – Supreet 2013-03-07 17:55:46

+0

我已發佈的答案檢查一次.. – Pragnani 2013-03-07 18:03:29

回答

3

doInBackground不是在UI線程中運行,但是你想從doInBackground

textViewDisplay = (TextView) findViewById(R.id.textViewDisplay); //wrong 

刪除此,並把它onPreExecute方法

得到視圖,可以真正做到UI操作非UI線程,所以不要在doinBackGround的textViewDisplay上執行任何操作。

最後將數據追加到字符串,並在doInBackground回來,你會得到onPostExecute作爲一個參數使用該值設置Textvalue 像這樣

public String doInBackGround() 
{ 
-- 
return result; 
} 
public void onPostExecute(String result) 
{ 
textViewDisplay.setText(result); 
} 
+0

你的意思是我會在InBackGround()中獲得所有聯繫人和數字,並將它們追加到OnPostExecute()方法中 – Supreet 2013-03-07 18:05:37

+1

將它們附加到一個字符串或將它們單獨保存在列表中,這是您的選擇。但你不應該在doInBackground中執行UI操作,你已經知道onPostExecute捕獲doInBackGround返回的結果,嘗試返回一個列表或字符串包含doInBackground中的聯繫人,並使用onPostExecute執行Ui操作,如在TextView中顯示 – Pragnani 2013-03-07 18:08:41

+0

非常感謝我瞭解 – Supreet 2013-03-07 18:10:21

1

我不在你的代碼上看不到任何問題,但進度對話可能爲空等。

我已經寫過顯示&隱藏函數並使用它們。我在調用asynctask(在activity中 - 實際上它們是我的基本活動)並在onPostExecute中hideLoading()之前調用showLoading()函數。

請嘗試。希望能幫助到你。

問候。

protected void showLoading() { 
    if (dialog == null) { 
     dialog = ProgressDialog.show(this, "", this.getResources() 
       .getString(R.string.loading)); 
     dialog.setMessage(this.getResources().getString(R.string.loading)); 
     dialog.setCancelable(true); 
     dialog.setCanceledOnTouchOutside(false); 
     dialog.setOnCancelListener(new OnCancelListener() { 

      public void onCancel(DialogInterface arg0) { 
       if(dialog.isShowing()) 
        dialog.dismiss(); 
       finish(); 
      } 
     }); 
     dialog.show(); 
    } 
} 

protected void hideLoading() { 
    if (dialog != null && dialog.isShowing()) { 
     dialog.dismiss(); 
     dialog = null; 
    } 
} 
1

這裏有幾件事情是錯誤的。我不能肯定地說,是什麼導致了泄漏,但是你追下來之前,解決這些問題:

  • 你真的不應該與textViewDisplay在後臺線程搞亂。如果您需要/想要在UI工作時更新UI,請使用publishProgress()來完成此操作。

  • 我沒有看到任何你現在稱之爲publishProgress()的地方。這意味着您的onProgressUpdate()方法可能永遠不會被調用。

+0

因爲我是新的android你會修改代碼如何使用publishProgress() – Supreet 2013-03-07 14:28:20

+1

由於你是Android的新手,你應該開始閱讀[文檔。](http://developer.android.com/reference/android/os/AsyncTask.html)這是編程的一個重要部分。 – Geobits 2013-03-07 14:38:51