简体   繁体   English

进度对话框不出现

[英]Progress Dialog does not appear

I have a fragment that, when is pressed a button should show a Progress Dialog and execute a method that contains an AsyncTask but when I press the button, the Progess Dialog does not appear on the screen but the async task is executed. 我有一个片段,当按下按钮时,应该显示进度对话框并执行包含AsyncTask的方法,但是当我按下按钮时,Progess对话框不会出现在屏幕上,而是执行了异步任务。 This is the code of the fragment 这是片段的代码

public class ParcheggiaFragment extends Fragment {

private Button button;
ProgressDialog progressDialog1;
SharedPreferences prefs;
HttpGetNoleggio httpGetNoleggio;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

    final View layout = inflater.inflate(R.layout.fragment_parcheggia, container, false);

    button = (Button) layout.findViewById(R.id.button);
    prefs = this.getActivity().getSharedPreferences(Keys.SHARED_PREFERENCES, Context.MODE_PRIVATE);
    button.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {


            progressDialog1.show();

            int a = checkNoleggioCompletato();

            FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
            fragmentTransaction.replace(R.id.frame_principale, new ResultFragment());
            fragmentTransaction.commit();

        }
    });

    return layout;
}

private int checkNoleggioCompletato() {

    final int id = prefs.getInt(Keys.ID_UTENTE, -1);

    Integer[] noleggio;
    int idNoleggio = -1;
    try {
        String str = "here i put the url that i have to use" + id; 
        URL url = new URL(str);

        noleggio = httpGetNoleggio.execute(url).get();
        idNoleggio = noleggio[0];
        int idBici = noleggio[1];
        int noleggioResult = idNoleggio;
    } catch (MalformedURLException | ExecutionException | InterruptedException e) {
        e.printStackTrace();
    }
    return idNoleggio;
}

@Override
public void onStart() {

    progressDialog1 = new ProgressDialog(this.getActivity());
    progressDialog1.setIndeterminate(true);

    super.onStart();
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    httpGetNoleggio = new HttpGetNoleggio(this.getActivity());

}

@Override
public void onResume() {
    super.onResume();
}

} }

The code seems correct and I can't understand why the ProgressDialog does not appear. 该代码似乎正确,我不明白为什么ProgressDialog没有出现。 HttpNoleggio is another class that extends AsyncTask and implements the doInBackground method HttpNoleggio是扩展AsyncTask并实现doInBackground方法的另一个类

As per your code you are displaying the progress bar in ParcheggiaFragment and then replacing this fragment with ResultFragment. 根据您的代码,您将在ParcheggiaFragment中显示进度条,然后用ResultFragment替换此片段。 As ResultFragment replaced ParcheggiaFragment so that progress bar is not displaying. 由于ResultFragment替换了ParcheggiaFragment,因此进度条不显示。 Just comment the following code: 只需注释以下代码:

FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.frame_principale, new ResultFragment());
fragmentTransaction.commit();

Then, progressbar will be visible. 然后,进度条将可见。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM