- Home /
errors with zxing barcode scanner
hey all, I get this parse error and some other errors when I want to implement the code for using the zxing barcode scanner.
this is the code I use:
using UnityEngine; using System.Collections;</p> <p>public class Scanner : MonoBehaviour {</p> <pre><code>// Use this for initialization void Start () { gameObject.Find("ScanBtn").setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); startActivityForResult(intent, 0); } }); } // Update is called once per frame void Update () { public void onActivityResult(int requestCode, int resultCode, Intent intent) { if (requestCode == 0) { if (resultCode == RESULT_OK) { String contents = intent.getStringExtra("SCAN_RESULT"); String format = intent.getStringExtra("SCAN_RESULT_FORMAT"); // Handle successful scan gameObject.Find("ScanBtn").Destroy(); } else if (resultCode == RESULT_CANCELED) { // Handle cancel } } } } </code></pre> <p>}
the errors that I get are these:
Assets/Scanner.cs(10,22): error CS1526: A new expression requires () or [] after type
Assets/Scanner.cs(10,42): error CS1525: Unexpected symbol `v'
Assets/Scanner.cs(15,18): error CS8025: Parsing error
any help would be greatly appreciated :)
Answer by Mike 3 · Nov 30, 2010 at 03:21 PM
Your problem is with the code in start - you're trying to declare a new function instead of declaring a delegate
I would fix it like this:
void Start () { gameObject.Find("ScanBtn").setOnClickListener(onClick); }
void onClick(View v) { Intent intent = new Intent("com.google.zxing.client.android.SCAN"); intent.putExtra("SCAN_MODE", "QR_CODE_MODE"); startActivityForResult(intent, 0); }
thanks a bunch, this helped me alot. Now I only need to figure out why it doesn't take view and intent...
This is handy for me too thanks- I do have a question for you, How on earth are you accessing Intents from C#? I'm pulling my hair out with code, searching the net etc, and in this snippet you're just using them straight off! Is there a library you're using? or have I completely missed what's going on here?
Your answer
