Android App没有执行按钮点击

时间:2014-12-20 20:25:58

标签: java android android-fragments android-studio

由于某些原因,单击ADD按钮时我的android应用程序将不会执行。 我正在使用片段你能帮助我并带领我朝着正确的方向前进吗?当我点击 添加按钮,没有任何反应,当字段为空时甚至没有第一个Toast语句

public class AthleteCreation extends Fragment implements View.OnClickListener{
    Communicator communicator;
    Button btnAdd;
    EditText editFirstName, editLastName, editAge, editTier;

    public AthleteCreation() {

    }
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

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

        final List<String> genderList=new ArrayList<String>();
        genderList.add("Male");
        genderList.add("Female");

        Spinner s = (Spinner) view.findViewById(R.id.spinnerGender);
        ArrayAdapter<String> dataAdapter = new ArrayAdapter<String>(getActivity(), R.layout.support_simple_spinner_dropdown_item, genderList);
        dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        s.setAdapter(dataAdapter);

       return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
    }

    public void initializeVariables(){
        btnAdd = (Button) getActivity().findViewById(R.id.btnAdd);
        editFirstName = (EditText) getActivity().findViewById(R.id.editFirstName);
        editLastName = (EditText) getActivity().findViewById(R.id.editLastName);
        editAge = (EditText) getActivity().findViewById(R.id.editAge);
        editTier = (EditText) getActivity().findViewById(R.id.editTier);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);

        initializeVariables();
        btnAdd.setOnClickListener(this);
    }

    @Override
    public void onDetach() {
        super.onDetach();
    }

    @Override
    public void onClick(View v) {
        switch(v.getId()){
            case R.id.btnAdd:
                Athlete athlete = new Athlete();

                if(editFirstName.getText().length() == 0 || editLastName.getText().length() == 0
                    || editAge.getText().length() == 0 || editTier.getText().length() == 0) {
                    Toast.makeText(getActivity(), "Fill in all fields!", Toast.LENGTH_SHORT).show();
                }
                else{
                    athlete.setFirstName(editFirstName.getText().toString());
                    athlete.setLastName(editLastName.getText().toString());
                    athlete.setAge(Integer.parseInt(editAge.getText().toString()));
                    athlete.setGender("");
                    athlete.setEvent("");
                    athlete.setTier(Integer.parseInt(editTier.getText().toString()));

                    Toast.makeText(getActivity(), athlete.toString(), Toast.LENGTH_LONG).show();
                    communicator = (Communicator) getActivity();
                    communicator.send(athlete);
                }
                break;
        }
    }
}

2 个答案:

答案 0 :(得分:1)

在initializeVariables()方法中,用getView()替换getActivity()。

btnAdd = (Button) getView().findViewById(R.id.btnAdd);

然后btnAdd.setOnClickListener(this)将有效。

答案 1 :(得分:1)

initializeVariables()中您使用的是getActivity().findViewById(R.id.foo)而不是使用片段的根视图,这可能是您未获得匹配案例的原因切换声明。

尝试:

 getView().findViewById(R.id.foo);