(我在Android中的第一步,请考虑到它:) 在我的应用程序中,我在一个Activity中使用片段来显示用户要回答的不同问题,并在三个图像按钮下方知道检查员如何考虑每个州,如图所示:
每个图像按钮显示不同的alertDialogs,它在第一个选项卡中运行良好,但是当我更改选项卡时,例如在选项卡2中,应用程序崩溃并在代码之前附加的日志中显示错误。
例如,这是片段Tab2的代码。
public class DepositoTab2 extends Fragment {
public DepositoTab2() {
// Required empty public constructor
}
RadioButton q6a1;
RadioButton q6a2;
RadioButton q6a3;
Button btn6;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_deposito_tab2, null);
// Inflate the layout for this fragment
return v;
}
@Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
q6a1 = (RadioButton)getView().findViewById(R.id.q6a1);
q6a2 = (RadioButton)getView().findViewById(R.id.q6a2);
q6a3 = (RadioButton)getView().findViewById(R.id.q6a3);
btn6 = (Button)getView().findViewById(R.id.btnq6);
ImageButton btnsindefecto = (ImageButton) getActivity().findViewById(R.id.SinDefecto);
ImageButton btndefectoleve = (ImageButton) getActivity().findViewById(R.id.DefectoLeve);
ImageButton btndefectograve = (ImageButton) getActivity().findViewById(R.id.DefectoGrave);
final SharedPreferences app_preferences = PreferenceManager.getDefaultSharedPreferences(getActivity());
btn6.setOnClickListener(new View.OnClickListener(){
public void onClick(View v){
((DepositoVisual)getActivity()).setCurrentItem (2, true);
SharedPreferences.Editor editor = app_preferences.edit();
if (q6a1.isChecked()){
editor.putInt("answer_value6", 0);
} else {
if (q6a2.isChecked()){
editor.putInt("answer_value6", 1);}
else {
if (q6a3.isChecked()){
editor.putInt("answer_value6", 2);
}
}
}
editor.commit();
}
});
btnsindefecto.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setIcon(R.drawable.icono);
builder.setTitle("Sin defecto");
builder.setMessage("Sin defecto si...Lorem ipsum dolor sit amet, consect");
builder.setNegativeButton("Entendido",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();}});
btndefectoleve.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setIcon(R.drawable.icono);
builder.setTitle("Defecto Leve");
builder.setMessage("Defecto Leve si...Lorem ipsum dolor sit amet, consect");
builder.setNegativeButton("Entendido",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
}});
btndefectograve.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
AlertDialog.Builder builder = new AlertDialog.Builder(
getActivity());
builder.setIcon(R.drawable.icono);
builder.setTitle("Defecto Grave");
builder.setMessage("Defecto Grave si...Lorem ipsum dolor sit amet, consect");
builder.setNegativeButton("Entendido",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
}
});
builder.setCancelable(false);
AlertDialog alert = builder.create();
alert.show();
}
});
}
这是错误日志
java.lang.IllegalStateException: Could not find a method onClick(View) in the activity class com.us.grupopal.inspeccionapp.DepositoVisual.DepositoVisual for onClick handler on view class android.widget.ImageButton with id 'DefectoLeve'
at android.view.View$1.onClick(View.java:4007)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)
Caused by: java.lang.NoSuchMethodException: onClick [class android.view.View]
at java.lang.Class.getMethod(Class.java:664)
at java.lang.Class.getMethod(Class.java:643)
at android.view.View$1.onClick(View.java:4000)
at android.view.View.performClick(View.java:4780)
at android.view.View$PerformClick.run(View.java:19866)
at android.os.Handler.handleCallback(Handler.java:739)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:135)
at android.app.ActivityThread.main(ActivityThread.java:5254)
at java.lang.reflect.Method.invoke(Native Method)
at java.lang.reflect.Method.invoke(Method.java:372)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:903)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:698)