Cryosec
Cryosec

Reputation: 1

Passed back bundle to mainactivity has null extras

I looked around on StackOverflow, but couldn't find something like my problem. I read about a similar problem on multiple questions, but they were all about a single extra in an intent. My problem is about a bundle of extras in the intent passed back from the second activity to the mainactivity.

here's the Main activity relevant snip:

FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);        
fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {

            Intent intent = new Intent(getApplicationContext(), addItem.class);
            startActivityForResult(intent, 1);

        }
    });

And:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    System.out.println("REQUEST CODE:" + requestCode);
    super.onActivityResult(requestCode, resultCode, data);
    Bundle extras = data.getExtras();
    String item_name = extras.getString("ITEM_NAME");
    int item_price = extras.getInt("ITEM_PRICE");
    int item_qty = extras.getInt("ITEM_QTY");


    ListView list = (ListView) findViewById(R.id.listView);
    ArrayList<String> arrayList = new ArrayList<String>();
    ArrayAdapter<String> adapter;

    adapter = new ArrayAdapter<String>(getApplicationContext(), android.R.layout.simple_spinner_item, arrayList);

    list.setAdapter(adapter);

    arrayList.add(item_name);
    adapter.notifyDataSetChanged();

}

Second Activity snip:

public class addItem extends AppCompatActivity{

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.add_item);

    Button btn1 = (Button)findViewById(R.id.button);

    btn1.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            addToList();

        }
    });

}

public void addToList(){

    Intent inty = new Intent();
    Bundle extras = new Bundle();

    EditText txt1 = (EditText)findViewById(R.id.textItem);
    String name = txt1.getText().toString();
    extras.putString("ITEM_NAME", name);

    EditText txt2 = (EditText)findViewById(R.id.textPrice);
    int price = Integer.parseInt(txt2.getText().toString());
    extras.putInt("ITEM_PRICE", price);

    EditText txt3 = (EditText)findViewById(R.id.textNum);
    int numb = Integer.parseInt(txt3.getText().toString());
    extras.putInt("ITEM_QTY", numb);

    setResult(RESULT_OK, inty);
    finish();
}

}

To be more specific, when i press the button to send back the data (from the three forms), the app crashes and android studio tells me:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=-1, data=Intent {  }} to activity {it.cryosec.appname/it.cryosec.appname.MainActivity}: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.os.Bundle.getString(java.lang.String)' on a null object reference

did I get something wrong?

Upvotes: 0

Views: 338

Answers (1)

Adam Miśtal
Adam Miśtal

Reputation: 735

You have to set bundle to your intent in second activity. intent.setExtras (bundle)

Upvotes: 1

Related Questions