2 clcurie 11 clcurie_11 于 2013.04.18 17:19 提问

从Activity A 传递到 Activity B 的值返回 null

我想从activity B中获取信息,然后传到activity A 中,问题是我获得的值是null。我的代码显示是你点击过多少次按钮然后把值返回到第一个Activity,的确应该是这个样子的。
我贴出代码,大家看看哪里出错了?

returned.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {

                Intent intent = new Intent(Next.this, Hw3Activity.class);
                intent.putExtra("text", counted.getText().toString());
                startActivity(intent);
/*Next is the current activity, Counted is the name of my text box*/
            }
        });
        click.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                counter++;
            }

这是我想把信息传递到的 Activity:

Button change;
    TextView text;
    int number;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        change = (Button) findViewById(R.id.change);
        text = (TextView) findViewById(R.id.count);
        String s1 = getIntent().getStringExtra("Textview01");
        text.setText("You clicked the button " + s1 + " times.");
        change.setOnClickListener(new View.OnClickListener() {
            public void onClick(View v) {
                Intent intent = new Intent(getApplicationContext(), Next.class);
                startActivityForResult(intent, 0);
/*this button is for going to the 2nd activity,not my problem currently*/
            }
        });
    }
}

3个回答

luhuajcdd
luhuajcdd   2013.04.19 09:30
已采纳
String s1 = getIntent().getStringExtra("Textview01");

应该为 :

String s1 = getIntent().getStringExtra("text");
guoleimail
guoleimail   2013.04.18 18:28
Intent intent = new Intent(getApplicationContext(), Next.class);
 //添加把次数加到intent中
 intent.putExtra("CLICK_NUMBER", times);
 private static final int RESULT = 1;
 startActivityForResult(intent, RESULT);

 public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (resultCode == RESULT) {
      //做你要做的事,解析次数
    }
 }

就是使用startActivityForResultonActivityResult
startActivityForResult 中不能为0,

baimy1985
baimy1985   2013.04.18 20:42
String s1 = getIntent().getStringExtra("Textview01");和`intent.putExtra("text", counted.getText().toString());`

取值和传值的字符串不一样,是笔误还是问题出在这里?

Csdn user default icon
上传中...
上传图片
插入图片
准确详细的回答,更有利于被提问者采纳,从而获得C币。复制、灌水、广告等回答会被删除,是时候展现真正的技术了!