我有一项任务是ProgressBar在其上添加带有百分比值的值,以在用户想要预测某些内容时显示一个值。我没有使用,ProgressDialog因为它现在已被弃用。此处,百分比值取决于从请求开始到完成所用的时间。我正在使用Volley从服务器获取数据。
这是我要实现的目标的示例图像:
我已经这样做了
我在警报对话框中实现了一个水平进度条(我使用这种方式是因为进度对话框已被弃用)。我想像这样设置进度条。
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.fragment_result_prediction, container, false);
predictProgress = (ProgressBar) view.findViewById(R.id.progressbarPredict);
AlertDialog.Builder(view.getContext());
builder.setCancelable(false); // if you want user to wait for some process to finish,
View v = inflater.inflate(R.layout.layout_loading_dialog, null);
builder.setView(v);
final AlertDialog dialog = builder.create();
submitPredictPlant.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
dialog.show();
predictProgress.setProgress(25);
predictProgress.setProgress(50);
predictProgress.setProgress(75);
predictProgress.setProgress(100);
dialog.dismiss();
Run Code Online (Sandbox Code Playgroud)
当进度为 100 时,我想关闭对话框。但这会出现类似错误
java.lang.NullPointerException: Attempt …Run Code Online (Sandbox Code Playgroud)