尽管密钥存在,为什么捆绑始终为空?

时间:2018-06-07 18:03:08

标签: java android

以下代码旨在使用在Signup.java中注册时收集的数据包将数据传递给显示数据的ViewProfile.java。在检查bundle中的键时,它返回true,但是,在ViewProfile.java中检查时,bundle为null。帮助将表示赞赏。

Signup.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);
        }
    });
}

}

1 个答案:

答案 0 :(得分:0)

您在SignUp活动中调用了错误的活动:

    if(b==true) {
            Intent i = new Intent(Signup.this, MainActivity.class);//problem here

您已将意图设置为MainActivity.class并将数据发送到MainActivity而不是ViewProfile活动。

在SignUp活动中更改为:

if(b==true) {
            Intent i = new Intent(Signup.this, ViewProfile.class);