Reputation: 183
I have a trouble with my app. I have an activity which name is PetActivity. In this activity are some values... and a button(update) too. This button will switch the user to another activity called UpdateActivity in which he can edit the values from PetActivity. There is a save button which should finish this activity and re-open the PetActivity. I've got error in this action... (null pointer exception)
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.project/com.example.project.PetActivity}: java.lang.NullPointerException
PetActivity onclick code:
if (v.getId() == R.id.button1updateAP) {
Intent i = new Intent(PetActivity.this, UpdateActivity.class);
Bundle ext = getIntent().getExtras();
String idOfPet = ext.getString(IDR);
i.putExtra(UpdateActivity.ID, idOfPet);
startActivity(i);
finish();
}
UpdateActivity onclick code:
if (v.getId() == R.id.button1saveAU) {
MyDatabaseHelper db = new MyDatabaseHelper(this);
Pet petU = new Pet();
petU.setID(id);
petU.setName(TVname.getText().toString());
petU.setAge((ETage.getText().toString()));
petU.setWeight(ETweight.getText().toString());
db.updatePet(petU);
db.close();
DatabaseWeight dbw = new DatabaseWeight(this);
dbw.addPetWeight(new Pet(petU.getID(), petU.getWeight()));
Intent i2 = new Intent(this, PetActivity.class);
startActivity(i2);
finish();
}
When i tried this, it was ok:
Intent i2 = new Intent(this, ListActiv.class);
startActivity(i2);
finish();
this code had switch me to List and the i could go to refreshed PetActivity,but I have to get straight to the PetActivity.
I would be very thankful for any suggestions.
Thanks!
Upvotes: 0
Views: 162
Reputation: 13364
You don't need to call finish() after launching UpdateActivity from PetActivity, and inside UpdateActivity when your work is done just call setResult() before finish()...
Your code should be for onClick of MainActivity
if (v.getId() == R.id.button1updateAP) {
Intent i = new Intent(PetActivity.this, UpdateActivity.class);
Bundle ext = getIntent().getExtras();
String idOfPet = ext.getString(IDR);
i.putExtra(UpdateActivity.ID, idOfPet);
startActivityForResult(i,some request code ););
finish();
}
For onClick of ChildActivity
if (v.getId() == R.id.button1saveAU) {
MyDatabaseHelper db = new MyDatabaseHelper(this);
Pet petU = new Pet();
petU.setID(id);
petU.setName(TVname.getText().toString());
petU.setAge((ETage.getText().toString()));
petU.setWeight(ETweight.getText().toString());
db.updatePet(petU);
db.close();
DatabaseWeight dbw = new DatabaseWeight(this);
dbw.addPetWeight(new Pet(petU.getID(), petU.getWeight()));
if(/*task completed successfully*/){
setResult(RESULT_OK);
}else{
setResult(RESULT_CANCELED);
}
finish();
}
Now in your Parent Activity you can check these values inside onActivityResult()
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.i(LOG_TAG, "Inside onActiviy Result : "+resultCode);
if ( resultCode == RESULT_OK ) {
Toast.makeText(this, "Update executed successfully..", Toast.LENGTH_SHORT).show();
}else if ( resultCode == RESULT_CANCELED ) {
Toast.makeText(this, "There was an error", Toast.LENGTH_SHORT).show();
}
}
Also see various intent flags in doc..
Upvotes: 2