在关闭android中的警告对话框后,为什么不进行微调器工作?

时间:2017-02-17 05:19:12

标签: android android-dialogfragment android-dialog

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

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

这就是我想要的。 当他们点击肯定时,它应该显示微调器并继续在后台调用服务。 当他们点击否定时,它应该取消选中该选项。

这是代码。

此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)

2 个答案:

答案 0 :(得分:0)

ProgressDialog progressDialog = new ProgressDialog(getActivity); 中,你必须使用:

getActivity()

documentation开始,Activity方法会返回FragmentActivity当前与之关联的内容。

修改: 在ProgressDialog progressDialog = new ProgressDialog(YourActivityClassName.this); 课程中,你必须使用:

public class DrawGradient extends View {

    Paint paint;
    LinearGradient lgA;
    LinearGradient lgB;
    ComposeShader shader;

    public DrawGradient(Context context) {
        super(context);
        initView();
    }

    private void initView() {
        setLayerType(View.LAYER_TYPE_SOFTWARE, null);
        paint = new Paint();
        paint.setAntiAlias(true);
        paint.setFilterBitmap(true);

    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        lgA = new LinearGradient(0, 0, w, h, 0xff000000, 0xffffffff, Shader.TileMode.CLAMP);
        lgB = new LinearGradient(0, h, w, 0, 0xffffffff, 0xff000000, Shader.TileMode.CLAMP);
        shader = new ComposeShader(lgA, lgB, PorterDuff.Mode.MULTIPLY);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        paint.setShader(shader);
        canvas.drawPaint(paint);
    }
}

答案 1 :(得分:0)

Context返回null

更改 Choose_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
       ...
    }

}

Choose_CountryFragment

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