进度对话框不会出现

时间:2016-09-01 17:28:06

标签: java android android-fragments android-asynctask progressdialog

我有一个片段,当按下按钮时,按钮应显示进度对话框并执行包含AsyncTask的方法,但是当我按下按钮时,Progess对话框不会出现在屏幕上,但执行了异步任务。这是片段的代码

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();
}

}

代码似乎是正确的,我无法理解为什么ProgressDialog没有出现。 HttpNoleggio是另一个扩展AsyncTask并实现doInBackground方法的类

1 个答案:

答案 0 :(得分:0)

根据您的代码,您将在ParcheggiaFragment中显示进度条,然后使用ResultFragment替换此片段。由于ResultFragment替换了ParcheggiaFragment,因此不显示进度条。只需评论以下代码:

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

然后,进度条将可见。