Android studio getIntent().getStringExtra() 返回 null [英] Android studio getIntent().getStringExtra() returns null

查看:56
本文介绍了Android studio getIntent().getStringExtra() 返回 null的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将一个活动的值发送到另一个活动.

I want to send a value from one activity to another.

值是从第一个活动发送的:

The value is sent from this first Activity:

 // Tout est Ok
Intent intent = new Intent(getApplicationContext(), SetNewPassword.class);
intent.putExtra("number", phone);

startActivity(intent);
finish(); 

我想在第二个活动中接收值,但是当我尝试在 Toast 中显示该值时,它显示 null:

I want to receive the value in this second Activity, but when i try to display the value in a toast it displays null:

phone = getIntent().getStringExtra("number");

Toast.makeText(SetNewPassword.this, phone, Toast.LENGTH_SHORT).show();

推荐答案

 // get the text to pass
    EditText editText = (EditText) findViewById(R.id.editText);
    String textToPass = editText.getText().toString();

    // start the SecondActivity
    Intent intent = new Intent(this, SecondActivity.class);
    intent.putExtra(Intent.EXTRA_TEXT, textToPass);
    startActivity(intent);

在第二个活动中

   // get the text from MainActivity
    Intent intent = getIntent();
    String text = intent.getStringExtra(Intent.EXTRA_TEXT);

你的数据类型有问题.检查这个或分享你的完整代码.

you have problem in data type .check this or share your full code .

这篇关于Android studio getIntent().getStringExtra() 返回 null的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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