2017-08-01 32 views
1

我已经添加的进步从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 

我在做什么错...请帮助..

+0

您需要删除所有这些ShowProgressDialog(); stopWithSuccess(); stopWithFailure();方法从onCreate并在需要时明确使用它 –

+0

其仍然给出相同的错误 –

+0

但我们可以在Dailog中直接创建。 –

回答

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

的主要活动? –

+0

你可以在你的公共实用程序类或基本活动类中使用它。或任何你想要的 –

+0

好吧...让我试试 –

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