Android DialogAlert show()错误

时间:2016-09-19 00:31:35

标签: android android-alertdialog

我正在尝试在我的Android应用程序中创建一个对话框aler但是在应该显示警告对话框的方法中我收到错误。

这是我的DialoBox类:

public class DialogBox extends DialogFragment implements View.OnClickListener, AdapterView.OnItemSelectedListener {

    private static final String TAG =  DialogBox.class.getSimpleName();

    private Button okButton;
    private Spinner ageSpinner;
    private RadioButton radioDialogMale;
    private RadioButton radioDialogFemale;
    private String selectedAge;

    private Communicator communicator;

    /** public void onAttach(Activity activity) {
            super.onAttach(activity);

        communicator  = (Communicator) activity;
    } **/

    public View onCreateView (LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

        Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Ini ");

        View view =  inflater.inflate(R.layout.dialog_box_layout, container, false);

        ageSpinner = (Spinner) view.findViewById(R.id.age_spinner_dialog);
        ageSpinner.setSelected(false);
        ageSpinner.setAdapter(populateAgeSpinner());
        ageSpinner.setOnItemSelectedListener(this);

        radioDialogMale = (RadioButton) view.findViewById(R.id.radioDialogM);
        radioDialogMale.setSelected(false);
        radioDialogMale.setOnClickListener(this);
        radioDialogFemale = (RadioButton) view.findViewById(R.id.radioDialogF);
        radioDialogFemale.setSelected(false);
        radioDialogFemale.setOnClickListener(this);

        okButton = (Button) view.findViewById(R.id.dialog_ok);
        okButton.setOnClickListener(this);

        communicator =  (LoginActivity) getActivity();

        setCancelable(false);

        Log.d(TAG, "onCreateView(LayoutInflaterm ViewGroup, Bundle) - Fi ");

        return view;
    }

    @Override
    public void onClick(View v) {

        Log.d(TAG, "onClick(View) - Ini ");
        switch (v.getId()) {

            case R.id.radioDialogF:
                radioDialogFemale.setSelected(true);
                break;
            case R.id.radioDialogM:
                radioDialogMale.setSelected(true);
                break;
            case R.id.dialog_ok:

                if(validate()) {

                    String gender = "M";
                    if(radioDialogFemale.isSelected()) {
                        gender =  "F";
                    }

                    communicator.onDialogValues(gender,Integer.parseInt(selectedAge));
                    dismiss();
                }
                break;


        }
        Log.d(TAG, "onClick(View) - Ini ");
    }

    // To fill the age spinner
    public ArrayAdapter<Integer> populateAgeSpinner () {

        Log.d(TAG ,"populateAgeSpinner - Ini");

        List age =  new ArrayList<String>();
        age.add("");

        for(int i =  18; i <= 100; i++) {
            age.add(Integer.toString(i));
        }

        ArrayAdapter<Integer> spinnerAgeAdapter = new ArrayAdapter<Integer>(getActivity(),R.layout.spinner_item_layout, age);
        spinnerAgeAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);

        Log.d(TAG ,"populateAgeSpinner - Fi");

        return spinnerAgeAdapter;

    }

    public boolean validate() {
        Log.d(TAG ,"validate() - Ini");

        boolean result =  true;

        if(selectedAge.equals(null) || selectedAge.isEmpty()) {
            TextView errorText = (TextView)ageSpinner.getSelectedView();
            errorText.setError(getResources().getString(R.string.Error_register_age));
            result =  false;
        }

        if(!radioDialogFemale.isSelected() && !radioDialogMale.isSelected()) {

            radioDialogFemale.setError(getResources().getString(R.string.Error_register_gender));
            radioDialogMale.setError(getResources().getString(R.string.Error_register_gender));
            result =  false;
        }

        Log.d(TAG ,"validate() - Fi");

        return result;

    }

    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        selectedAge = parent.getItemAtPosition(position).toString();

    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

  public   interface  Communicator {
         void onDialogValues(String gender, int age);
    }
}

这是我收到错误的地方:

  public class LoginActivity extends BaseActivity implements GoogleApiClient.OnConnectionFailedListener, View.OnClickListener
        ,DialogBox.Communicator {


........


  public void showDialog() {

        FragmentManager fm =  getSupportFragmentManager();
         DialogBox myDialog =  new DialogBox();
         myDialog.show(fm,"MyDialog");
    }

它不接受myDialog.show(fm, "MyDialo")知道DialogBox类扩展DialogFragment

1 个答案:

答案 0 :(得分:0)

我决定改变:FragmentManager fm = getSupportFragmentManager();

 FragmentManager fm =  getFragmentManager();