java - 尽管存在密钥,为什么捆绑始终为空?
问题描述
下面的代码旨在使用 bundle 将在 Signup.java 中注册时收集的数据传递到显示数据的 ViewProfile.java。在检查包中的键时,它返回 true,但是,在 ViewProfile.java 中检查时,包为空。帮助将不胜感激。
注册.java
public class Signup extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup);
final EditText n=(EditText)findViewById(R.id.editText3);
final EditText u=(EditText)findViewById(R.id.editText4);
final EditText p=(EditText)findViewById(R.id.editText5);
final EditText c=(EditText)findViewById(R.id.editText6);
Button s=(Button)findViewById(R.id.button4);
final Userdatabase udb=new Userdatabase(this);
s.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
String name=n.getText().toString();
String email=u.getText().toString();
String password=p.getText().toString();
String phone=c.getText().toString();
boolean b=udb.insertuser(name,email,password);
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);
Bundle bundle=new Bundle();
bundle.putString("NAME",name);
bundle.putString("ID",email);
bundle.putString("PHONE",phone);
i.putExtras(bundle);
startActivity(i);
}
else
Toast.makeText(getApplicationContext(),"Please try again",Toast.LENGTH_SHORT).show();
}
});
}
}
ViewProfile.java
public class ViewProfile extends AppCompatActivity {
String name,username,contact,profession;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_view_profile);
Intent intent=getIntent();
Bundle b=intent.getExtras();
if(b!=null) {
name = b.getString("NAME");
username = b.getString("ID");
contact = b.getString("PHONE");
TextView tv1=(TextView)findViewById(R.id.textView17);
TextView tv2=(TextView)findViewById(R.id.textView18);
TextView tv3=(TextView)findViewById(R.id.textView19);
tv1.setText(name);
tv2.setText(username);
tv3.setText(contact);
}
ImageView img=(ImageView)findViewById(R.id.imageView4);
img.setOnClickListener(new View.OnClickListener()
{
public void onClick(View v)
{
Intent i=new Intent(ViewProfile.this,Profile.class);
startActivity(i);
}
});
}
}
解决方案
您在 SignUp 活动中调用了错误的活动:
if(b==true) {
Intent i = new Intent(Signup.this, MainActivity.class);//problem here
您已将意图设置为 MainActivity.class 并将数据发送到 MainActivity 而不是 ViewProfile 活动。
在注册活动中更改为:
if(b==true) {
Intent i = new Intent(Signup.this, ViewProfile.class);
推荐阅读
- typescript - 使用方法参数类型时,打字稿环境警告总是希望类型联合中的每个键
- python - 无法在win server2019上为vscode设置python解释器
- c++ - 不同组中具有相同名称的子组
- javascript - Angular 将错误的注入器注入到动态组件中?
- squirrel-sql - Squirrel SQL:(并行)连接数
- python - 如何测量边缘之间的垂直距离?
- laravel - 过滤查询未正确运行
- javascript - 动态填充
- python - 写入 mongodb - socket.timeout:超时
- dynamics-crm - Dynamics 365 Workflow 将所有记录的字段设置为“是”