RestClient
という名前のクラスがWebServiceから情報を取得して戻ってきて、インターネットにアクセスしている間に進捗ダイアログを実行しようとしています。そして、私はこのクラスを複数の場所で使うので、私はアクティビティそのものには入れません。ここに私のRestClient
クラスです:アクティビティから呼び出されたときに別のAsyncTaskクラスのProgressDialogが表示されない
public class RestClient extends AsyncTask<URL, String, String> {
private Context context;
private String string;
public RestClient(Context context, String string)
{
this.context = context;
this.string = string;
}
@Override
protected void onPreExecute() {
dialog = ProgressDialog.show(context, "Buscando seu Produto","Por favor, espere um momento...",true ,false);
//I've already tried:
/*ProgressDialog dialog = new ProgressDialog(context);
dialog.setTitle("Buscando seu Produto");
dialog.setMessage("Por favor, espere um momento...");
dialog.setIndeterminate(true);
dialog.setCancelable(false);*/
dialog.show();
super.onPreExecute();
}
@Override
protected String doInBackground(URL... params) {
try {
//Some WebService gets and Json conversions using my string variable
//and some Thread.sleep that counts 2000 miliseconds to do all the queries
dialog.dismiss();
} catch (IOException | InterruptedException |JSONException e) {
e.printStackTrace();
dialog.dismiss();
return e.getMessage();
}
return null;
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
}
}
そして、私はこのようなボタンをクリックしたときに、私の活動に私はクラスRestClient
を呼び出す:
---編集:私は、私はAlertDialogでを持っていることを言及するのを忘れてしまいました時々ProgressDialogの前後に表示することができ、この同じ活性---
private Button buttonConfirm;
private EditView evString;
private String theString;
private String returnFromExecute;
private RestClient restClient;
private AlertDialog.Builder dialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_access_webservice);
evString = (EditText) findViewById(R.id.editViewMyString);
buttonConfirm = (Button) findViewById(R.id.buttonConfirm);
dialog = new ProgressDialog(IdentificacaoDeProdutoActivity.this);
dialog.setTitle("Error");
dialog.setMessage("Please try again");
dialog.setIndeterminate(true);
dialog.setCancelable(false);
buttonConfirmar.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
theString = evString.getText().toString();
if(!(theString!=null && theString.trim().length()>0)) //To check if theString is not null
{
dialog.show();
}
restClient = new RestClient(AccessWebserviceActivity.this, theString);
//Then I call execute and put a Thread.sleep a bit longer to compensate the ones I have in my doInBackground
restClient.execute();
try {
Thread.sleep(2050);
} catch (Exception e) {
dialog.show();
return;
}
}
}
}
は、問題は私のProgressDialog
を示したことがないということです。私はすでにの代わりにgetParent()
,getApplication()
とgetApplicationContext()
を試してみましたが、何も働いていません。誰か何が起きているのか、私は何をすべきでしょうか?
あなたのAsyncTaskクラスがあなたのActivityクラスの子か、AsnctaskClassが別のパッケージを持っていると言えますか? – Hamza
@AmeerHamza、それは他のアクティビティで使用するため、私のアクティビティの子ではありませんが、私のアクティビティは同じパッケージに含まれていません。 – Cocholate