【问题标题】:Progress Dialog does not appear进度对话框不出现
【发布时间】:2016-09-01 17:28:06
【问题描述】:

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

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 方法的类

【问题讨论】:

  • 你在哪里删除对话框。可能任务完成得太快以至于对话框甚至没有时间显示。
  • @Shaishav 任务需要 7 秒才能完成。 ProgressDialog 没有出现在这个片段中,但出现在下一个片段中ResultFragment
  • 我不明白,你说“Progess Dialog 没有出现在屏幕上”。您不是仅在单击按钮时才开始 ResultFragment 吗?
  • @Shaishav 我忘了说当片段更改时会出现 ProgressDialog。在这种情况下,它会在 ResultFragment 出现在屏幕上时出现
  • 再一次,你说“执行一个包含AsyncTask的方法”。这是AsyncTask 在哪里?新片段应该立即启动(当您开始一个新事务时),并且在上面应该出现进度条。这不是你打算做的吗?如果不是,你为什么要开始交易?

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


【解决方案1】:

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

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

然后,进度条将可见。

【讨论】:

  • 但是,他正在使用活动的上下文初始化进度对话框,所以无论fragment 显示什么,它不应该出现(并且行为合法)吗?
  • 片段更改时出现ProgressDialog。在这种情况下,它会在 ResultFragment 出现在屏幕上时出现
  • 根据代码进度条是可见的,但在这个新的 ResultFragment 上方出现是因为新事务。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-08-07
  • 1970-01-01
  • 2016-09-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多