为什么在我的片段活动中只有第一个按钮正常工作?当我按下其他人时没有任何事情发生。
没有错误。
public class FifthgradeFragment extends Fragment implements OnClickListener {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
Button b = (Button) rootView.findViewById(R.id.but5th1);
b.setOnClickListener(this);
Button b1 = (Button) rootView.findViewById(R.id.but5th2);
b.setOnClickListener(this);
Button b2 = (Button) rootView.findViewById(R.id.but5th3);
b.setOnClickListener(this);
Button b3 = (Button) rootView.findViewById(R.id.but5th4);
b.setOnClickListener(this);
Button b4 = (Button) rootView.findViewById(R.id.but5th5);
b.setOnClickListener(this);
Button b5 = (Button) rootView.findViewById(R.id.but5th6);
b.setOnClickListener(this);
Button b6 = (Button) rootView.findViewById(R.id.but5th7);
b.setOnClickListener(this);
Button b7 = (Button) rootView.findViewById(R.id.but5th8);
b.setOnClickListener(this);
Button b8 = (Button) rootView.findViewById(R.id.but5th9);
b.setOnClickListener(this);
return rootView;
}
@Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.but5th1:
Intent myIntent = new Intent(getActivity(), C51.class);
getActivity().startActivity(myIntent);
break;
case R.id.but5th2:
Intent Intent = new Intent(getActivity(), C52.class);
getActivity().startActivity(Intent);
break;
case R.id.but5th3:
Intent Intent1 = new Intent(getActivity(), C53.class);
getActivity().startActivity(Intent1);
break;
case R.id.but5th4:
Intent Intent2 = new Intent(getActivity(), C54.class);
getActivity().startActivity(Intent2);
break;
case R.id.but5th5:
Intent Intent3 = new Intent(getActivity(), C55.class);
getActivity().startActivity(Intent3);
break;
case R.id.but5th6:
Intent Intent4 = new Intent(getActivity(), C56.class);
getActivity().startActivity(Intent4);
break;
case R.id.but5th7:
Intent Intent5 = new Intent(getActivity(), C57.class);
getActivity().startActivity(Intent5);
break;
case R.id.but5th8:
Intent Intent6 = new Intent(getActivity(), C58.class);
getActivity().startActivity(Intent6);
break;
case R.id.but5th9:
Intent Intent7 = new Intent(getActivity(), C59.class);
getActivity().startActivity(Intent7);
break;
}
}}
答案 0 :(得分:1)
这是你错了 所有b.setOnClickListener(this);已分配:P
b.setOnClickListener(this);
Button b1 = (Button) rootView.findViewById(R.id.but5th2);
<强> b.setOnClickListener(本); 强> 按钮b2 =(按钮)rootView.findViewById(R.id.but5th3); 的 b.setOnClickListener(本); 强> 按钮b3 =(按钮)rootView.findViewById(R.id.but5th4); 的 b.setOnClickListener(本); 强> 按钮b4 =(按钮)rootView.findViewById(R.id.but5th5); 的 b.setOnClickListener(本); 强> 按钮b5 =(按钮)rootView.findViewById(R.id.but5th6); 的 b.setOnClickListener(本); 强> 按钮b6 =(按钮)rootView.findViewById(R.id.but5th7); 的 b.setOnClickListener(本); 强> 按钮b7 =(按钮)rootView.findViewById(R.id.but5th8); 的 b.setOnClickListener(本); 强> 按钮b8 =(按钮)rootView.findViewById(R.id.but5th9); 的 b.setOnClickListener(本); 强>
不要复制粘贴:P使用一些逻辑:)祝你好运