注册 登录
编程论坛 Android开发

intent传递数据空指针问题

无果分 发布于 2016-03-12 17:35, 4995 次点击
刚开始学安卓,做了一个简单的用户注册界面,点击浏览按钮在另外一个页面显示数据,但总是提示空指针,我反复看了好几遍也找不出哪里错,求指教
第一个是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);
    }
1 回复
#2
SKYYYF2017-02-13 14:21
改错步骤:
1.看错误提示信息,找到错误点
2.按照错误提示信息修正错误,继续调试运行。
3.如果有错误,回到第一条。
4.正常运行。
1