简体   繁体   中英

Switching activities/passing data between activities

So using suggestions from the last question I asked, I figured out how to call BarCodeScanner, and return the value to a field. so now, I have a toast that says "successful scan" and then I want to pass the result to a new activity. when I comment out my intent, everything works (minus the passing of data/switching of screen, obviously) but when I run my project as is, it FC's... no errors reported by eclipse in code or XML. Any insights?

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;
  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();


    Intent i = new Intent(main.this, Result.class);
        i.putExtra("SNARP", "SCAN_RESULT");
        startActivityForResult(i, 0);

      } else if (resultCode == RESULT_CANCELED) {

        if ( totalbox != null )
          totalbox.setText("bummer");
      }
    }      
  }

}

And then to handle the data being passed, in the new activity:

package com.mhe.test.scan;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Result extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);

    Intent i = getIntent();
    Bundle b = i.getExtras();
    String foosh = b.getString("SNARP");

    EditText box1 = (EditText) findViewById(R.id.tBox1);     
       box1.setText(foosh); 

Try sending a Bundle object when calling the new intent.

Intent i = new Intent(main.this, Result.class);
Bundle b = new Bundle();
b.putString("SNARP", "SCAN_RESULT")
i.putExtras(b);

Try getting string in the child Activity this way.

public class Result extends Activity {
  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result);

    Intent i = getIntent();
    String foosh = i.getStringExtra("SNARP");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM