【发布时间】: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