gpt4 book ai didi

java - Android 应用程序不执行按钮单击

转载 作者:行者123 更新时间:2023-11-29 20:56:23 24 4
gpt4 key购买 nike

由于某些原因,当单击“添加”按钮时,我的 android 应用程序将不会执行。我正在使用 fragment 你能帮助我并引导我朝着正确的方向前进吗?当我点击添加按钮,没有任何反应,当字段为空时甚至没有第一个 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;
}
}
}

最佳答案

initializeVariables() 中,您使用的是 getActivity().findViewById(R.id.foo) 而不是使用 Fragment 的 Root View ,这可能是您不要在 switch 语句中得到匹配的大小写。

尝试:

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

关于java - Android 应用程序不执行按钮单击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27584029/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com