user1162494
user1162494

Reputation: 43

Error whilst implementing zxing

I am using a tutorial to implement the zxing code into my own application. I have followed the code available, however i am receiving an error, can anyone suggest how to fix the error please?

package meena.com.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MeenaActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       Button but_previous=(Button)findViewById(R.id.button1);
       but_previous.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) { 
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);

                **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
                          } else if (resultCode == RESULT_CANCELED) {
                             // Handle cancel
                          }
                      }
                    }



    }
       });
    }

}

The line of code with an error is : public void onActivityResult(int requestCode, int resultCode, Intent intent); {

I am being told there is syntax error and misplaced constructors, however this is the code given i am unsure of how to fix it. The tutorial i have used is "http://damianflannery.wordpress.com/2011/06/13/integrate-zxing-barcode-scanner-into-your-android-app-natively-using-eclipse/"

Upvotes: 0

Views: 323

Answers (1)

FoamyGuy
FoamyGuy

Reputation: 46856

onActivityResult() is a method of the Activity class. In order to override it you need to move that chunk of code outside of your on create block. Like this:

    package meena.com.android;

import android.app.Activity;
import android.os.Bundle;
import android.widget.Button;

public class MeenaActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       Button but_previous=(Button)findViewById(R.id.button1);
       but_previous.setOnClickListener(new OnClickListener() {
                public void onClick(View arg0) { 
                    Intent intent = new Intent("com.google.zxing.client.android.SCAN");
                    intent.putExtra("SCAN_MODE", "QR_CODE_MODE");
                    startActivityForResult(intent, 0);

                }
       });
    }

    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
            } else if (resultCode == RESULT_CANCELED) {
                // Handle cancel
            }
        }
    }

}

Upvotes: 1

Related Questions