我的进度栏只是一个旋转器。
我正在向用户显示两个选项,当用户单击一个选项时,我会显示一个对话框,以便用户读取并理解它。
这就是我想要的。当他们单击正面时,它应该显示旋转器并在后台保持呼叫服务。当他们单击负面时,应该使该选项不选中。
这是代码。
此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)
您的Context
返回null
在中更改代码> select_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
...
}
}
Override onattach Choose_CountryFragment
@Override
public void onAttach(Context context) {
super.onAttach(context);
mContext = context;
}
在Fragment
中,您必须使用:
ProgressDialog progressDialog = new ProgressDialog(getActivity);
从文档中,getActivity()
方法返回Activity
此Fragment
当前与之关联。
编辑:在Activity
类中,您必须使用:
ProgressDialog progressDialog = new ProgressDialog(YourActivityClassName.this);