2017-02-14 162 views
0

我創建了包含ProgressDialog及其方法的Activity。android在構造函數中創建ProgressDialog

public class ProgressActivity extends AppCompatActivity implements Thread.UncaughtExceptionHandler { 

    public final ProgressDialog progressDialog; 

    public void dismissPD() { 
     if (progressDialog != null && progressDialog.isShowing()) { 
      progressDialog.dismiss(); 
     } 
    } 

    public void setPDMessage(String msg) { 
     progressDialog.setMessage(msg); 
    } 

    public void showPD(String msg) { 
     ProgressDialog.show(this, "", msg); 
    } 

    @Override 
    public void uncaughtException(Thread thread, Throwable ex) { 
     ex.printStackTrace(); 
    } 

    public ProgressActivity() { 
     progressDialog = new ProgressDialog(this); 
    } 

    @Override 
    protected void onDestroy() { 
     super.onDestroy(); 
     dismissPD(); 
    } 

} 

現在,我從它延長我的新的活動,但我得到NullpointerException內constructur上progressDialog = new ProgressDialog(this) 依我之見,我有一個錯誤的上下文構造運行時。我有沒有正確的方法來實現它?

+0

progressDialog =新ProgressDialog(youractivity.this) – Sasi

+1

progressDialog =新ProgressDialog(本);將此移至活動創建方法。 –

+0

我想在父活動上取得進展Dialog private。那可能嗎? – Rainmaker

回答

0

我通過重寫oncreate評判主活動的解決了這個問題:

@Override 
protected void onCreate(@Nullable Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    progressDialog = new ProgressDialog(this); 
}