ProgressDialog 没有出现在 OnPreExecute 中

ProgressDialog doesn't appear inside OnPreExecute

我正在尝试在解析 JSON 对象期间创建 ProgressDialog。所以我正在使用 AsyncTask

    private class JsonUtility extends AsyncTask<String, Void, String> {

    //getting activity context like that since outer activity is not enclosing
    public JsonUtility(Activity activity){
        super();
        mActivity = activity;

    }

    public JsonUtility(){}

    @Override
    protected void onPreExecute(){
        super.onPreExecute();
        dialog = ProgressDialog.show(mActivity, "Loading...", "Please wait...", true);
    }

    @Override
    protected String doInBackground(String... params) {

//...operate with JSON

    }

    @Override
    protected void onPostExecute(String s) {
        super.onPostExecute(s);
        dialog.hide();
    }
}

我从 JSON 那里得到这样的信息:

    public ArrayList<CoursesData> getCourses(Activity activity){
    ArrayList<CoursesData> resultArrayList = new ArrayList<>();
    try {
        JSONArray jArray = new JSONArray(new JsonUtility(activity).execute(coursesUrl).get());
//...and so on
}
当使用 getActivity():

创建 Fragment 时,

activity 传递给 JsonUtility

ArrayList<CoursesData> coursesDatas = parserJson.getCourses(getActivity());

从 Fragment 传递 activity 上下文的方式是否正确?

或者问题可能出在其他地方?尽管加载大约需要 2-3 秒,但此 ProgressDialog 根本不会出现。

请帮我解决这个问题。

将 mActivity 更改为 getParent()...它对我有用