so, i’ve been working on this same stupid thing for a while now. some folks here have helped me get it to the point it is but now i’ve got to move farther forward… but first, my code:
package com.mhe.test.scan;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class main extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button myScanButton = (Button) findViewById(R.id.myScanButton);
totalbox = (EditText) findViewById(R.id.tBox);
myScanButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent("com.google.zxing.client.android.SCAN");
intent.putExtra("SCAN_MODE", "PRODUCT_MODE");
startActivityForResult(intent, 0);
}
});
}
private EditText totalbox;
@Override
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if (requestCode == 0) {
if (resultCode == RESULT_OK) {
final String contents = intent.getStringExtra("SCAN_RESULT");
if ( totalbox != null );
totalbox.setText(contents);
Context context = getApplicationContext();
CharSequence text = "Successful Scan";
int duration = Toast.LENGTH_SHORT;
Toast toast = Toast.makeText(context, text, duration);
toast.show();
Button myTotalButton = (Button) findViewById(R.id.myTotalButton);
myTotalButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent pass = new Intent(view.getContext(), Result.class);
startActivityForResult(pass, 0);
}
});
} else if (resultCode == RESULT_CANCELED) {
if ( totalbox != null );
totalbox.setText("bummer");
}
}
}
}
so anyhow, what i’d like to happen is upon a successful scan, the result is loaded into the EditText totalbox. then the ‘myTotalButton’ is clicked and will pass the result to the next activity ‘Result.class’. right now i’m just trying to get it to switch to the new activity. If the
Button myTotalButton = (Button) findViewById(R.id.myTotalButton);
myTotalButton.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
Intent pass = new Intent(view.getContext(), Result.class);
startActivityForResult(pass, 0);
code is there, it FCs. otherwise, the rest of it works fine. any suggestions/assistance would be helpful. I feel like i am missing something stupid that i will smack myself for.
I think I just realized what the problem is.
The button press activity calls back into the same
onActivityResultmethod it’s called from, and using the same requestCode of 0. This is causing the errors where you attempt togetStringExtra("SCAN_RESULT")because that only exists in the callback from the scan.Instead of
startActivityForResult(pass, 0);usestartActivityForResult(pass, 1);(or whatever digit) and handle it by adding ancode section to the existing requestCode if statement (or make it a switch() statement).
The end result would look something like this: