在活动之间切换活动/传递数据

时间:2010-06-30 22:40:47

标签: java android

因此,根据我提出的上一个问题提出的建议,我想出了如何调用BarCodeScanner,并将值返回给字段。所以现在,我有一个叫“成功扫描”的祝酒词然后我想把结果传递给一个新的活动。当我评论我的意图时,一切正常(减去数据的传递/屏幕的切换,显然)但是当我按原样运行我的项目时,它是FC的...没有错误由eclipse在代码或XML中报告。任何见解?

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");
      }
    }      
  }

}

然后在新活动中处理传递的数据:

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

2 个答案:

答案 0 :(得分:1)

尝试在调用新意图时发送Bundle对象。

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

答案 1 :(得分:0)

尝试以这种方式在子Activity中获取字符串。

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