I think you have a couple of problems with how you've structured your code. By the look of it, you still have duplicate code, and you're not calling the generic code. Also, the generic code should not be an onClick override.
You should create a separate Java class that's not an activity or view - just a generic class. Let's call it Lib. Then, in that class, you define a static function as your click handler:
Code:
import android.content.Context;
public class Lib {
public static final String ERROR = "ERROR";
private Lib() {}
public static void scannerClickHandler(View v, Context context) {
zXingScannerView = new ZXingScannerView(context);
setContentView(zXingScannerView);
zXingScannerView.setResultHandler( (ZXingScannerView.ResultHandler) context );
ScancodeActivity.scan(zXingScannerView);
}
}
Then your click handlers would both be like this:
Code:
@Override
public void onClick(View v) {
Lib.scannerClickHandler(v, this);
}
Done in the same way but am getting error can you please see this error.
Process: com.codeexap.ews, PID: 22934
java.lang.ClassCastException: com.codeexap.ews.Display cannot be cast to me.dm7.barcodescanner.zxing.ZXingScannerView$ResultHandler
at com.codeexap.ews.ScancodeActivity$lib.scannerClickHandler(ScancodeActivity.java:263)
at com.codeexap.ews.Display.onClick(Display.java:140)
at android.view.View.performClick(View.java:4848)
at android.view.View$PerformClick.run(View.java:20299)
at android.os.Handler.handleCallback(Handler.java:815)
at android.os.Handler.dispatchMessage(Handler.java:104)
at android.os.Looper.loop(Looper.java:218)
at android.app.ActivityThread.main(ActivityThread.java:5657)
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:990)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:785)
This is the code i have changed .
scancodeactivity.java
Code:
@Override
public void onClick(View v) {
lib.scannerClickHandler( v,this );
setContentView(zXingScannerView);
}
public static class lib
{
public lib() {
}
public static void scannerClickHandler (View v, Context context){
zXingScannerView = new ZXingScannerView( context );
zXingScannerView.setResultHandler( (ZXingScannerView.ResultHandler) context );
zXingScannerView.startCamera();
}
}
Display.java
Code:
@Override
public void onClick(View v) {
ScancodeActivity.lib.scannerClickHandler( v,this );
setContentView(zXingScannerView);
}