0

私はアンドロイドが初めてで、アンドロイドであるアクティビティから別のアクティビティに移動するときに進行状況を表示したいのですが、コードは次のとおりです。 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();
}
}
4

3 に答える 3

3

doInBackground は UI スレッドで実行されませんが、doInBackground からビューを取得しようとしています

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

これを削除して onPreExecute メソッドに配置します

また、非 UI Thread で実際に UI 操作を実行できるため、doinBackGround の textViewDisplay で操作を実行しないでください。

最後に、データを文字列に追加して doInBackground に戻すと、onPostExecute でその値をパラメーターとして取得し、それを使用して Textvalue を次のように設定します

public String doInBackGround()
{
--
return result;
}
public void onPostExecute(String result)
{
textViewDisplay.setText(result);
}
于 2013-03-07T18:03:03.770 に答える
1

コードに問題はありませんが、progressdialog が null などである可能性があります。

私は表示と非表示の機能を作成し、それらを使用しています。asynctask を実行する前に showLoading() 関数を呼び出します (実際には、それらは私の baseactivity にあります) と onPostExecute で hideLoading() を呼び出します。

試してみてください。それが役に立てば幸い。

よろしく。

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;
    }
} 
于 2013-03-07T12:52:58.567 に答える
1

ここにはいくつか問題があります。リークの原因ははっきりとは言えませんが、それを突き止める前に、次の問題を修正してください。

  • textViewDisplayバックグラウンド スレッドをいじってはいけません。作業中に UI を更新する必要がある場合は、それを使用publishProgress()してください。

  • 今、あなたが呼んでいる場所はどこにもありませんpublishProgress()。つまり、onProgressUpdate()メソッドが呼び出されない可能性が高いということです。

于 2013-03-07T14:08:59.330 に答える