切换活动/活动之间传递数据 [英] Switching activities/passing data between activities

查看:114
本文介绍了切换活动/活动之间传递数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此​​,使用从最后一个问题,我问意见,我想通了,如何调用酒吧codeScanner,值返回到域中。所以现在,我有一个敬酒,说:成功的扫描,然后我要的结果传递给新的活动。当我注释掉我的意图,一切正常(减去数据/屏幕切换的传球,很明显),但是当我跑我的项目是,它FC的......没有错误在code或XML蚀的报道。任何见解?

 包com.mhe.test.scan;进口android.app.Activity;
进口android.content.Context;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;
进口android.widget.Toast;
公共类主要活动扩展{
 / **当第一次创建活动调用。 * /
 @覆盖
 公共无效的onCreate(捆绑savedInstanceState){
 super.onCreate(savedInstanceState);
 的setContentView(R.layout.main);
   按钮myScanButton =(按钮)findViewById(R.id.myScanButton);
   totalbox =(EditText上)findViewById(R.id.tBox);     myScanButton.setOnClickListener(新Button.OnClickListener(){
       公共无效的onClick(视图v){
         意向意图=新意图(com.google.zxing.client.android.SCAN);
         intent.putExtra(SCAN_MODE,PRODUCT_MODE);
         startActivityForResult(意向,0);
      }
   });
  }
  私人的EditText totalbox;
  公共无效的onActivityResult(INT申请code,INT结果code,意图意图){
    如果(要求code == 0){
      如果(结果code == RESULT_OK){
      最终字符串内容= intent.getStringExtra(SCAN_RESULT);        如果(totalbox!= NULL)
         totalbox.setText(内容);
        上下文的背景下= getApplicationContext();
        CharSequence的文字=成功扫描;
        INT持续时间= Toast.LENGTH_SHORT;
        吐司面包= Toast.makeText(背景,文本,持续时间);
        toast.show();
    意图I =新意图(main.this,Result.class);
        i.putExtra(SNARP,SCAN_RESULT);
        startActivityForResult(I,0);      }否则如果(结果code == RESULT_CANCELED){        如果(totalbox!= NULL)
          totalbox.setText(无赖);
      }
    }
  }}

和再处理所传递的数据,在新的活动:

 包com.mhe.test.scan;进口android.app.Activity;
进口android.content.Intent;
进口android.os.Bundle;
进口android.view.View;
进口android.widget.Button;
进口android.widget.EditText;公共类结果延伸活动{
  / **当第一次创建活动调用。 * /
  @覆盖
  公共无效的onCreate(捆绑savedInstanceState){
    super.onCreate(savedInstanceState);
    的setContentView(R.layout.result);    意向I = getIntent();
    束B = i.getExtras();
    串foosh = b.getString(SNARP);    的EditText BOX1 =(EditText上)findViewById(R.id.tBox1);
       box1.setText(foosh);


解决方案

尝试调用新的意图时,发送捆绑对象。

 意向书I =新意图(main.this,Result.class);
束B =新包();
b.putString(SNARP,SCAN_RESULT)
i.putExtras(二);

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

这篇关于切换活动/活动之间传递数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆