简体   繁体   中英

Why does this throw a 'null object reference' error?

I'm trying to pass a string from another activity to this one and then send it into an array and then into a listview. whenever i run this i get "Attempt to invoke virtual method 'android.os.Bundle android.content.Intent.getExtras()' on a null object reference" it seems like the error has something to do with the fourth line with the 'extras' bundle but how is that a null object reference i defined it right there no?

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);
    Bundle extras = intent.getExtras();

    switch(requestCode) {
        case ACTIVITY_EDIT:
            String title = extras.getString(add.TITLE);
            String password = extras.getString(add.PASSWORD);
            adapter.add(title);
            break;
    }


}

Here's where the 'PASSWORD' and 'TITLE' variables are defined in the other Activity:

public void onClick(View v) {
    EditText titleBox = (EditText)findViewById(R.id.titleText);
    TITLE = titleBox.getText().toString();

    EditText passBox = (EditText)findViewById(R.id.passwdText);
    String pass = passBox.getText().toString();

    EditText confBox = (EditText)findViewById(R.id.editText3);
    String conf = confBox.getText().toString();

    if (pass.equals(conf)) {

        PASSWORD = pass;
        this.finish();

    } else {
        Toast.makeText(this, "Passwords don't match", Toast.LENGTH_SHORT);
    }

}

When you pass data from one activity to another using a Bundle, the data is received inside onCreate() method of the second activity not inside onActivityResult() unless you've specifically implemented that.

Check this answer on how to start another activity and how to pass data to another activity : https://stackoverflow.com/a/20170125/1239966

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM