我正在尝试从一个活动中获取edittext中的值,并尝试发送该值并在另一个活动中接收该值。但我无法得到这些价值观。但是,在输入值之后,我没有打开接收活动。我在登录后打开了这个活动。代码如下
MainActivity.java
Toast.makeText(MainActivity.this, "Successful Registration", Toast.LENGTH_SHORT).show();
Intent i = new Intent(MainActivity.this, Login_Activity.class);
String name,mobile;
name = Username.getText().toString();
mobile = Usermob.getText().toString();
Bundle i2 = new Bundle();
i2.putString("uname", name);
i2.putString("unumber",mobile);
i.putExtras(i2);
startActivity(i);
/*Toast.makeText(MainActivity.this, "captcha not match", Toast.LENGTH_SHORT).show();*/
referralPage.java
package com.mws.tms_application;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.EditText;
import android.widget.TextView;
public class referralPage extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_referral_page);
// MainActivity mn=new MainActivity();
TextView tv= (TextView) findViewById(R.id.referralcode);
EditText ed1= (EditText) findViewById(R.id.username_edtext);
EditText ed2= (EditText) findViewById(R.id.usermob_no_edtext);
Intent intent = getIntent();
/* String name = intent.getExtras().getString("uname");
String mobile = intent.getExtras().getString("unumber");*/
/*
name=ed1.getText().toString();
name=name.substring(0,3);
number=ed2.getText().toString();
number=number.substring(7,9);*/
Bundle bundle = getIntent().getExtras();
System.out.print("INtent called");
if (bundle != null) {
String name = bundle.getString("uname");
System.out.print("INtent runned");
tv.setText(name/*+bundle.getExtras().getString("unumber")*/);
}
else{
tv.setText("Your referral code");
}
// tv.setText(new StringBuilder().append(name).append(mobile).toString());
}
}
答案 0 :(得分:0)
尝试发送此类数据
String name,mobile;
name = Username.getText().toString();
mobile = Usermob.getText().toString();
Intent i = new Intent(MainActivity.this, Login_Activity.class);
i.putExtra("name", name);
i.putExtra("mobile", mobile);
startActivity(i);
现在接收此类
等其他活动中的数据String name = getIntent().getExtras().getString("name");
String mobile = getIntent().getExtras().getString("mobile");
答案 1 :(得分:0)
您的代码中指定的活动名称不正确。您必须指定要在其中传递数据的活动的名称。在这里,您创建了Intent以启动“Login_Activity.java”,而数据处理代码则以“referralPage.java”编写。
替换
Intent i = new Intent(MainActivity.this, Login_Activity.class);
与
Intent i = new Intent(MainActivity.this, referralPage.class);