创建一个用于各种活动的通用进度对话框

时间:2017-08-01 05:29:44

标签: android

我已经从GitHub添加了进度对话框的依赖项..我正在尝试创建一个包含进度对话框的公共活动....

public class CustomProgressDialog extends AppCompatActivity {

AnimatedCircleLoadingView animatedCircleLoadingView;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_custom_progress_dialog);
    animatedCircleLoadingView = (AnimatedCircleLoadingView) findViewById(R.id.circle_loading_view);
    ShowProgressDialog();
    stopWithSuccess();
    stopWithFailure();
}

public void ShowProgressDialog(){
    animatedCircleLoadingView.startDeterminate();
}

public void stopWithSuccess(){
    animatedCircleLoadingView.stopOk();
}

public void stopWithFailure(){
    animatedCircleLoadingView.stopFailure();
}
}

我在其他活动中使用的代码是......

public class MainActivity extends AppCompatActivity {

CustomProgressDialog customProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    customProgressDialog = new CustomProgressDialog();
    DialogMethod();
   }


   public void DialogMethod(){
      customProgressDialog.ShowProgressDialog();
 }
}   

我收到的错误是......

java.lang.NullPointerException: Attempt to invoke virtual method 'void com.github.jlmd.animatedcircleloadingview.AnimatedCircleLoadingView.startDeterminate()' on a null object reference

我做错了什么......请帮忙......

2 个答案:

答案 0 :(得分:1)

你不能这样调用它,因为AnimatedCircleLoadingView尚未初始化。做下面的事情

 public static void showLoading(Context context) {
    builder = new Dialog(context);
    builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
    builder.getWindow().setBackgroundDrawable(
            new ColorDrawable(Color.WHITE));
    builder.setCanceledOnTouchOutside(true);
    builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
        @Override
        public void onDismiss(DialogInterface dialogInterface) {
            //nothing;
        }
    });
    LayoutInflater inflater = LayoutInflater.from(context);
    View dialog_view = inflater.inflate(R.layout.fragment_mermaids_loading, null);
    PulsatorLayout p = (PulsatorLayout) dialog_view.findViewById(R.id.pulsator);
    builder.addContentView(dialog_view, new RelativeLayout.LayoutParams(
            ViewGroup.LayoutParams.MATCH_PARENT,
            ViewGroup.LayoutParams.MATCH_PARENT));

    builder.getWindow().setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.MATCH_PARENT);
    if (!((AppCompatActivity) context).isFinishing()) {
        builder.show();
        p.start();
    }
}

答案 1 :(得分:0)

我不太清楚为什么你想要一个单独的活动进行进度对话。这听起来像是不必要的工作,但我会让你做出判断。

我个人使用以下方法

private ProgressDialog setupProgressDialog(Context context) {
    ProgressDialog progress = new ProgressDialog(context);
    progress.setTitle(context.getString(R.string.label_progress_title));
    progress.setMessage(context.getString(R.string.label_progress_payment));
    progress.setProgressStyle(ProgressDialog.STYLE_SPINNER);
    progress.setCancelable(false);

    return progress;
}

我可以初始化并显示使用;

mProgressDialog = setupProgressDialog(mContext);
mProgressDialog.show();

当我的代码加载时,我使用;

if (mProgressDialog.isShowing()) {
    mProgressDialog.dismiss();
}

现在关于调试代码,不需要在onCreate中执行所有方法。这些最好单独使用,就像你在MainActivity中一样。

您可以在此处进一步了解ProgressDialog; ProgressDialog - Android Developer