2017-07-13 39 views
0

我正在實施使用Android材料步進器的步進器庫(https://github.com/stepstone-tech/android-material-stepper)。在第三步中,我正在實施一個BlockingStep,以便在用戶界面被阻止/顯示一個對話框時進行Http調用。AlertDialog不顯示在步進片段

問題是,對話框不顯示。我嘗試了ProgressDialog和AlertDialog,但是它們都沒有出現在屏幕上。

@Override 
public void onNextClicked(final StepperLayout.OnNextClickedCallback callback) { 
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity()); 
    builder.setView(R.layout.dialog_loader); 
    builder.setCancelable(false); 
    final AlertDialog dialog = builder.show(); 
    new Handler().postDelayed(new Runnable() { 
     @Override 
     public void run() { 
      try { 
       //Generating JSONObject 

       //Using AsyncTask to end JSON 
       TCPConfigurationTask task = new TCPConfigurationTask(); 
       String[] params = {mIp, mPort, configObject.toString(), "true"}; 
       String response = task.execute(params).get(); 

       //Consuming response 
       if (response != null) { 
        Log.d("JSON_CODE_RESPONSE", response); 
       } 
      } catch (JSONException | InterruptedException | ExecutionException e) { 
       jsonError = true; 
      } 
      dialog.dismiss(); 
      callback.goToNextStep(); 
     } 
    }, 0L); 
} 

編輯:在回答中提供

代碼是罰款。問題在於MainThread在等待AsyncTask做出響應時做了太多工作。

回答

3

替換這一行,

final AlertDialog dialog = builder.show(); 

AlertDialog alert = builder.create(); 
alert.show(); 

用如下駁回對話框,

alert.dismiss(); 

使用此,設置自定義佈局在您的AlertDialog,

builder.setView(R.layout.dialog_loader); 

LayoutInflater inflater = this.getLayoutInflater(); 
View dialogView = inflater.inflate(R.layout. dialog_loader, null); 
builder.setView(dialogView); 
+1

正是我的想法 – PrisonMike

+0

我需要保持AlertDialog作爲最終,這樣我就可以關閉它,當線程完成。 – Minoru

+0

是的,那是我說,你可以關閉線程中的alertdialog使用dismiss –