为什么';在android中关闭警报对话框后进度微调器是否工作?

为什么';在android中关闭警报对话框后进度微调器是否工作?,android,android-dialogfragment,android-dialog,Android,Android Dialogfragment,Android Dialog,我的进度条只是一个微调器 我向用户显示两个选项,当用户单击一个选项时,我会显示一个对话框,以便用户阅读和理解它 这就是我想要的。 当他们点击肯定时,它会显示微调器并在后台继续调用服务。 当他们单击“负数”时,应使选项处于未选中状态 这是代码 此radioGroup.setOnClickListener进入片段的onCreateView方法 public class Choose_CountryFragment extends Fragment { private Radi

我的进度条只是一个微调器

我向用户显示两个选项,当用户单击一个选项时,我会显示一个对话框,以便用户阅读和理解它

这就是我想要的。 当他们点击肯定时,它会显示微调器并在后台继续调用服务。 当他们单击“负数”时,应使选项处于未选中状态

这是代码

此radioGroup.setOnClickListener进入片段的onCreateView方法

    public class Choose_CountryFragment extends Fragment {
        private RadioGroup radioGroup;
        private TextView textView;
        private String countryChosen = null;
        ConnectionStatus connectionStatus = new ConnectionStatus(getContext());
        public Choose_CountryFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 final Bundle savedInstanceState) {
            View rootView =  inflater.inflate(R.layout.fragment_choose__country, container, false);

          radioGroup = (RadioGroup) rootView.findViewById(R.id.country_choice_radio);
         radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
                {
                    public void onCheckedChanged(RadioGroup group, int checkedId) {
                        switch(checkedId){
                            case R.id.countryCanada:
                                // do operations specific to this selection
                                countryChosen = "Canada";
                                Intent explicitServiceIntent = new Intent(getActivity(), Service.class);
                                explicitServiceIntent.putExtra("country", "Canada");
                                getActivity().startService(explicitServiceIntent);
                                connectionStatus.showProgress();
                                break;
                            case R.id.countryUSA:
                                countryChosen = "USA";
                                Dialog dialog = onCreateDialog(savedInstanceState);
                                dialog.show();
                                connectionStatus.showProgress();
                                break;
                        }
                    }
                });

        public Dialog onCreateDialog(final Bundle savedInstanceState) {
            // Use the Builder class for convenient dialog construction
            AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
            Toast.makeText(getContext(), "Click Got it", Toast.LENGTH_LONG).show();
            builder.setMessage(R.string.SignUpWarningInfo)
                    .setPositiveButton(R.string.gotIt, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {
                            Intent explicitServiceIntentUSA = new Intent(getActivity(), Service.class);
                            explicitServiceIntentUSA.putExtra("country", countryChosen );
                            getActivity().startService(explicitServiceIntentUSA);

                        }
                    })
                    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
                        public void onClick(DialogInterface dialog, int id) {                        
                            return;
                        }
                    });
            // Create the AlertDialog object and return it
            return builder.create();
        }
    }
}
ConnectionStatus.java

public class ConnectionStatus {

    private Context _context;

    private ProgressDialog progressDialog = null;

    public ConnectionStatus(Context context) {
        this._context = context;
    }
    public void showProgress(){
            progressDialog = new ProgressDialog(_context);
            progressDialog.setCancelable(false);
            progressDialog.setIndeterminate(true);
            progressDialog.show();
        }
}
单击“美国”时出错。 我犯了一个错误

         java.lang.NullPointerException: Attempt to invoke virtual method 'android.content.res.Resources$Theme android.content.Context.getTheme()' on a null object reference
                  at android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)
                  at android.app.AlertDialog.<init>(AlertDialog.java:109)
                  at android.app.ProgressDialog.<init>(ProgressDialog.java:77)
                  at com.a2.a2.ConnectionStatus.showProgress(ConnectionStatus.java:66)
                  at com.a2.a2.signUp.Choose_CountryFragment$1.onCheckedChanged(Choose_CountryFragment.java:73)
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.content.res.Resources$Theme android.content.Context.getTheme()”
在android.app.AlertDialog.resolveDialogTheme(AlertDialog.java:154)上
在android.app.AlertDialog.(AlertDialog.java:109)
在android.app.ProgressDialog.(ProgressDialog.java:77)
com.a2.a2.ConnectionStatus.showProgress(ConnectionStatus.java:66)
在com.a2.a2.signUp.Choose\u CountryFragment$1.onCheckedChanged(Choose\u CountryFragment.java:73)

片段中,您必须使用:

ProgressDialog progressDialog = new ProgressDialog(getActivity);
从中,方法返回当前与其关联的
活动
片段

编辑: 在
活动
类中,您必须使用以下命令:

ProgressDialog progressDialog = new ProgressDialog(YourActivityClassName.this);

片段中
,您必须使用:

ProgressDialog progressDialog = new ProgressDialog(getActivity);
从中,方法返回当前与其关联的
活动
片段

编辑: 在
活动
类中,您必须使用以下命令:

ProgressDialog progressDialog = new ProgressDialog(YourActivityClassName.this);

您的
Context
返回
null

中更改代码,选择\u CountryFragment

public class Choose_CountryFragment extends Fragment {
    ...
   protected Context mContext;
   private ConnectionStatus connectionStatus
   ...

    public Choose_CountryFragment() {
    }

      @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_choose__country, container, false);
       ...
       connectionStatus = new ConnectionStatus(mContext);// initialize ConnectionStatus here
       ...
    }

}
覆盖onAttach内部
选择\u CountryFragment

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}

您的
Context
返回
null

中更改代码,选择\u CountryFragment

public class Choose_CountryFragment extends Fragment {
    ...
   protected Context mContext;
   private ConnectionStatus connectionStatus
   ...

    public Choose_CountryFragment() {
    }

      @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             final Bundle savedInstanceState) {
        View rootView =  inflater.inflate(R.layout.fragment_choose__country, container, false);
       ...
       connectionStatus = new ConnectionStatus(mContext);// initialize ConnectionStatus here
       ...
    }

}
覆盖onAttach内部
选择\u CountryFragment

@Override
public void onAttach(Context context) {
    super.onAttach(context);
    mContext = context;
}


Fragment
Activity
类中包含此方法的位置?在Fragment中。为什么不包含当前工作?你能解释一下吗?你在片段上使用像:
Context-mContext
之类的方法吗?
getActivity()
方法,要获得它所附加的活动,并且在活动的实例方法中需要
Context
,你可以使用
this
。首先,为什么我应该使用
ProgressDialog
而不是
Dialog
Fragment
Activity
类中包含此方法?在Fragment中。为什么不是当前工作?你能解释一下吗?你在片段上使用像:
Context-mContext
之类的方法吗?
getActivity()
方法,要获得它所附加的活动,并且在活动的实例方法中需要
Context
,你可以使用
this
。首先,为什么我应该使用
ProgressDialog
而不是
Dialog
post-full
Choose\u CountryFragment
code@kishorejethava完成。完整发布
选择\u CountryFragment
code@kishorejethava好的。我在这里试图理解。当您说,“您的
上下文
返回
null
”ConnectionStatus ConnectionStatus=newconnectionstatus(getContext())//您的意思是说getContext()正在返回null,是谁将上下文传递给了onAttach()?我是个新手。对不起,问题太多了。好的。如何在Choose\u Country\u片段中调用onAttach()?因为
Choose\u CountryFragment扩展了片段。探索
Fragment
了解知识OK。我在这里试图理解。当您说,“您的
上下文
返回
null
”ConnectionStatus ConnectionStatus=newconnectionstatus(getContext())//您的意思是说getContext()正在返回null,是谁将上下文传递给了onAttach()?我是个新手。对不起,问题太多了。好的。如何在Choose\u Country\u片段中调用onAttach()?因为
Choose\u CountryFragment扩展了片段。探索
片段
获取知识