[英]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. 因此,根据我问的最后一个问题的建议,我弄清楚了如何调用BarCodeScanner并将值返回到字段。 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. 当我注释掉我的意图时,一切正常(显然,减去了数据的传递/屏幕的切换),但是当我按原样运行我的项目时,它是FC的... eclipse在代码或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时,尝试发送Bundle
对象。
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. 尝试以这种方式在子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");
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.