2016-11-22 130 views
0

使用以下用於示出進度對話框代碼,進度對話框顯示不

public void showDialog() { 
    try { 
     Log.d(TAG, "showDialog--------------"); 
     progressDialog = new Dialog(RewardsActivity.this); 
     final View dialogView = LayoutInflater.from(RewardsActivity.this).inflate(R.layout.progress_dialog, null); 
     progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
     progressDialog.setContentView(dialogView); 

     progressDialog.setCancelable(true); 

     final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress); 

     final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     anim.setRepeatCount(Animation.INFINITE); 
     anim.setDuration(500); 
     anim.setInterpolator(new LinearInterpolator()); 
     progressSpinner.startAnimation(anim); 

     progressDialog.show(); 
     Log.d(TAG, "showDialog--------------"); 
     progressDialog.getWindow().setLayout(120, 120); 
    } catch (Exception e) { 

    } 
} 

在Activity類

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_rewards); 

    toolbar = (Toolbar) findViewById(R.id.toolbar); 
    setSupportActionBar(toolbar); 

    getSupportActionBar().setDisplayHomeAsUpEnabled(true); 

    tinyDB = new TinyDB(getApplicationContext()); 
    showDialog(); 
    new GetRewardsResponse(getApplicationContext(), this, tinyDB); 


    viewpager = (ViewPager) findViewById(R.id.viewPager); 
    indicator = (CircleIndicator) findViewById(R.id.indicator); 
    indicator.setViewPager(viewpager); 
    if (progressDialog.isShowing()) { 
     progressDialog.hide(); 
    } 

} 

在調試的onCreate調用showDialog,執行showDaialog方法(對話是不可見雖然)完全但當應用程序正在運行它不會執行showDialog,因爲我無法看到我的日誌語句在Android顯示器

+1

嘗試評論if(progressDialog.isShowing()){ progressDialog.hide(); }並檢查 – kevz

回答

1

試試這個

public void showDialog() { 
    try { 
     Log.d(TAG, "showDialog--------------"); 
     progressDialog = new Dialog(RewardsActivity.this); 
     final View dialogView = LayoutInflater.from(RewardsActivity.this).inflate(R.layout.progress_dialog, null); 
     progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
     progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
     progressDialog.setContentView(dialogView); 

     progressDialog.setCancelable(true); 

     final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress); 

     final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
     anim.setRepeatCount(Animation.INFINITE); 
     anim.setDuration(500); 
     anim.setInterpolator(new LinearInterpolator()); 
     progressSpinner.startAnimation(anim); 

     progressDialog.show(); 
     Log.d(TAG, "showDialog--------------"); 
    } catch (Exception e) { 

    } 
} 

也從您的oncreate刪除此行。

if (progressDialog.isShowing()) { 
     progressDialog.hide(); 
    } 
+0

其實自己弄明白了,在設置ViewPager後解散對話框 – musica

0

刪除以下行或給予一定的時間延遲隱藏progressDialog,因爲的ShowDialog()是顯示對話框,並突然關閉它,

if (progressDialog.isShowing()) { 
     progressDialog.hide(); 
    } 
0

我已經在你的代碼的一些變化。

private Context mContext; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_rewards); 
     toolbar = (Toolbar) findViewById(R.id.toolbar); 
     setSupportActionBar(toolbar); 
     getSupportActionBar().setDisplayHomeAsUpEnabled(true); 
     tinyDB = new TinyDB(getApplicationContext()); 
     showDialog(); 
     new GetRewardsResponse(getApplicationContext(), this, tinyDB); 
     viewpager = (ViewPager) findViewById(R.id.viewPager); 
     indicator = (CircleIndicator) findViewById(R.id.indicator); 
     indicator.setViewPager(viewpager); 
     if (progressDialog.isShowing()) { 
      progressDialog.hide(); 
     } 
    } 

    public void showDialog() { 
     try { 
      Log.d(TAG, "showDialog--------------"); 
      LayoutInflater inflater = getLayoutInflater(); 
      progressDialog = new Dialog(mContext); 
      final View dialogView = inflater.inflate(R.layout.progress_dialog, null); 
      progressDialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE); 
      progressDialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent); 
      progressDialog.setContentView(dialogView); 
      progressDialog.setCancelable(true); 
      final ImageView progressSpinner = (ImageView) dialogView.findViewById(R.id.ivProgress); 
      final RotateAnimation anim = new RotateAnimation(0f, 350f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f); 
      anim.setRepeatCount(Animation.INFINITE); 
      anim.setDuration(500); 
      anim.setInterpolator(new LinearInterpolator()); 
      progressSpinner.startAnimation(anim); 
      progressDialog.getWindow().setLayout(120, 120); 
      progressDialog.show(); 
      Log.d(TAG, "showDialog--------------"); 
     } catch (Exception e) { 

     } 
    }