我是android的新手。我想将用户输入从一个活动发送到另一个活动,当我输入文本并点击sendText按钮时,我返回到主要活动,但我得到了“#null;'在textview中的结果应该是。看了其他的答案,但仍然没有快乐!
主要活动,接收来自其他活动的用户输入
TextView textView1;
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView1 = (TextView) findViewById(R.id.textView1);
}
public void relativeLayout(View view)
{
// Create The Intent and Start The Activity to get The message
Intent relativeLayoutIntent=new Intent(MainActivity.this,ExplicitRelativeLayout.class);
startActivityForResult(relativeLayoutIntent, 2);
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data)
{
super.onActivityResult(requestCode, resultCode, data);
// check if the request code is same as what is passed. here it is 2
if(requestCode==2)
{
if(null!=data)
{
// fetch the message String
String name = data.getStringExtra("name");
String email = data.getStringExtra("email");
String phone = data.getStringExtra("phone");
// Set the message string in textView
textView1.setText("Name: " + name + "" + "\n" + "Email: " + email + " " + "\n" +"Phone:" + phone );
}
RelativeLayout Activity,用户输入文本
public class ExplicitRelativeLayout extends AppCompatActivity {
EditText setName;
EditText setEmail;
EditText setPhone;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.explicitrelativelayout);
// Get the reference of Edit Text
setName = (EditText) findViewById(R.id.setName);
setEmail = (EditText) findViewById(R.id.setEmail);
setPhone = (EditText) findViewById(R.id.setPhone);
}
public void sendText (View views)
{
// get the Entered message
String name=setName.getText().toString();
String email= setEmail.getText().toString();
String phone=setPhone.getText().toString();
Intent intentmessage = new Intent();
// put the message in Intent
intentmessage.putExtra("Name:",name);
intentmessage.putExtra("Email:",email);
intentmessage.putExtra("Phone:",phone);
// Set The Result in Intent
setResult(2,intentmessage);
// finish The activity
finish();
}
}
答案 0 :(得分:3)
问题在于您创建keyString
看看
//put the message in Intent
intentmessage.putExtra("Name:",name);
intentmessage.putExtra("Email:",email);
intentmessage.putExtra("Phone:",phone);
在此putExtra
keyString
为Name:
,Email:
和Phone:
,您正试图通过此keyString
获取字符串
// fetch the message String
String name = data.getStringExtra("name");
String email = data.getStringExtra("email");
String phone = data.getStringExtra("phone");
您正在寻找name
作为keyString
,但没有任何内容,因为您决定将其称为Name:
查看putExtra(String name, Bundle value);
字符串是:
附加数据的名称,包前缀。
值是:
Bundle数据值。
希望它有所帮助。
答案 1 :(得分:1)
Intent类携带的所有内容都是带有HashMap结构的额外数据,该结构可以找到具有给定id的项目。您的问题是您为两个不同的Intent类中的每个项目设置了不同的ID,例如:
相对活动:
intentmessage.putExtra("Name:",name);
您的ID是“姓名:”
在您的主要活动中:
String name = data.getStringExtra("name");
您的身份证是“姓名”。
确保你在意图类中使用的两个id都相同,并且它将起作用。
答案 2 :(得分:1)
您在两个班级中使用的按键似乎不匹配
使用intentmessage.putExtra("name",name);
而不是intentmessage.putExtra("Name:",name);
答案 3 :(得分:0)
Activity B:
@Override
public void onBackPressed() {
// TODO Auto-generated method stub
//data send to Activity B
Intent intent = new Intent();
intent.putExtra("MESSAGE", strtext + "");//your msg
setResult(2, intent);
}
Activiy A:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String sSuName = data.getStringExtra("MESSAGE");
//txtfavouratecount.setText(sSuName);
}
答案 4 :(得分:0)
更改ExplicitRelativeLayout activity ::
的代码
- 的setResult(RESULT_OK,意图);
- 同时保留两项活动。
醇>
public void sendText (View views){
// get the Entered message
String name=setName.getText().toString();
String email= setEmail.getText().toString();
String phone=setPhone.getText().toString();
Intent intentmessage = new Intent();
// put the message in Intent
intentmessage.putExtra("name:",name);
intentmessage.putExtra("email:",email);
intentmessage.putExtra("phone:",phone);
// Set The Result in Intent
setResult(RESULT_OK,intentmessage);
// finish The activity
finish();
}