Reputation: 17
im doing an app for my project but im not sure what to pass in my method.
Heres how i call the method:
public void scanBar(View v) {
switch (v.getId()) {
case R.id.barcode_scan_btn:
try {
Intent intent = new Intent(ACTION_SCAN);
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
} catch (ActivityNotFoundException anfe) {
showDialog(getActivity(), "No Scanner Found", "Download the scanner application?", "Yes", "No").show();
}
break;
}
}
And here is my method:
private static AlertDialog showDialog(final Activity act, CharSequence title, CharSequence message, CharSequence buttonYes, CharSequence buttonNo) {
AlertDialog.Builder downloadDialog = new AlertDialog.Builder(act);
downloadDialog.setTitle(title);
downloadDialog.setMessage(message);
downloadDialog.setPositiveButton(buttonYes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
Uri uri = Uri.parse("market://search?q=pname:" + "com.google.zxing.client.android");
Intent intent = new Intent(Intent.ACTION_VIEW, uri);
try {
act.startActivity(intent);
} catch (ActivityNotFoundException anfe) {
}
}
});
downloadDialog.setNegativeButton(buttonNo, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
return downloadDialog.show();
}
Here is the error:
09-20 05:20:31.774 17032-17032/app.psiteportal.com.psiteportal E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: app.psiteportal.com.psiteportal, PID: 17032
java.lang.IllegalStateException: Could not find a method scanBar(View) in the activity class app.psiteportal.com.psiteportal.DrawerMainAdapter for onClick handler on view class android.support.v7.widget.AppCompatButton with id 'barcode_scan_btn'
at android.view.View$1.onClick(View.java:3994)
at android.view.View.performClick(View.java:4756)
at android.view.View$PerformClick.run(View.java:19761)
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:5253)
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:899)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:694)
Caused by: java.lang.NoSuchMethodException: scanBar [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:3987)
I dont know if where should i put my method either in the activity or in the fragment. pls help
EDIT: Do i need to change the parameter to Fragment or just Activity?
Upvotes: 0
Views: 421
Reputation: 733
The problem is that your method scanBar()
was declared as onClick callback (inside your layout). This won't work in fragments.
"The Activity hosting the layout must then implement the corresponding method." - developer.android.com
Its not a bug. If you would like to implement callback in this way, you must use activities instead of fragments.
So the solutions is to set an onClick listener, like this:
setOnClickListener(new OnClickListener() {/* Some Code */ });
Upvotes: 1