我是 android 新手,我想在 android 中从一个活动移动到另一个活动时显示进度。这里是代码。异常是“活动 com.example.mytest.ReadContactsActivity 已泄漏窗口 com.android.internal.policy.impl。 PhoneWindow$DecorView{40ce4900 VE.... R....I. 0,0-295,62} 最初添加在这里”
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();
}
}