intent传递数据空指针问题
刚开始学安卓,做了一个简单的用户注册界面,点击浏览按钮在另外一个页面显示数据,但总是提示空指针,我反复看了好几遍也找不出哪里错,求指教第一个是MainActivity.java,第二个是RegMsg.java
程序代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); txtUsername = (EditText) findViewById(R.id.txtUsername); txtPassword = (EditText) findViewById(R.id.txtPassword); btnLook = (Button)findViewById(R.id.btnLook); manRadio = (RadioButton)findViewById(R.id.radioMale); womanRadio = (RadioButton)findViewById(R.id.radioFemale); txtEmail = (EditText) findViewById(R.id.txtEmail); btnLook = (Button)findViewById(R.id.btnLook); btnLook.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this,RegMsg.class); Bundle bundle = new Bundle(); bundle.putString("username",txtUsername.getText().toString()); bundle.putString("password",txtPassword.getText().toString()); Log.v("-------------密码为-----------------",txtUsername.getText().toString()); String str=""; if(manRadio.isChecked()) str="男"; else if(womanRadio.isChecked()) str="女"; bundle.putString("sex",str); bundle.putString("email",txtEmail.getText().toString()); intent.putExtras(bundle); startActivity(intent); } }); }
程序代码:
protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Intent intent=this.getIntent(); Bundle bundle = intent.getExtras(); String username = bundle.getString("username"); String password = bundle.getString("password"); String sex = bundle.getString("sex"); String email = bundle.getString("email"); TextView viewUser = (TextView) findViewById(R.id.lbl); viewUser.setText("用户名:"+username+" 密码:"+password+" 性别:"+sex+" 邮件地址:"+email); Log.v("------------------用户名为--------------",username); }