0

I am trying to get a quiet simple activity to work. my activity includes two buttons, and i am trying to make one of them work as a simple back button (go back to previous activity) any help?

here is my code:

 public void valider(View v)
{


    c.setNomPrenom(et1.getText().toString());
    c.setmPortable(et2.getText().toString());
    c.setmFixe(et3.getText().toString());

    i.putExtra(ContactActivity.TAG_AJOUT_EDIT, c);

    setResult(RESULT_OK,i);

    finish();
}

@Override
public void onBackPressed() {

    super.onBackPressed();
}

public void retour(View v)
{
    onBackPressed();
}

Here is what LogCat shows:

    02-21 20:03:46.286: W/dalvikvm(1221): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
    02-21 20:03:46.336: E/AndroidRuntime(1221): FATAL EXCEPTION: main
    02-21 20:03:46.336: E/AndroidRuntime(1221): java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=1, result=0, data=null} to activity {hd.android.contact/hd.android.contact.ContactActivity}: java.lang.NullPointerException
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2980)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread.handleSendResult(ActivityThread.java:3023)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread.access$1100(ActivityThread.java:123)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1177)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.os.Handler.dispatchMessage(Handler.java:99)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.os.Looper.loop(Looper.java:137)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread.main(ActivityThread.java:4424)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at java.lang.reflect.Method.invokeNative(Native Method)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at java.lang.reflect.Method.invoke(Method.java:511)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at dalvik.system.NativeStart.main(Native Method)
    02-21 20:03:46.336: E/AndroidRuntime(1221): Caused by: java.lang.NullPointerException
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at    hd.android.contact.ContactActivity.onActivityResult(ContactActivity.java:132)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.Activity.dispatchActivityResult(Activity.java:4649)
    02-21 20:03:46.336: E/AndroidRuntime(1221):     at android.app.ActivityThread.deliverResults(ActivityThread.java:2976)

Thank you in advance.

Edit:

i also tried this but it didn't help:

        EditText et1,et2,et3;
Contact c;
Intent i=new Intent();
@Override
protected void onCreate(Bundle savedInstanceState) {

    super.onCreate(savedInstanceState);

    setContentView(R.layout.contact_edit);

     c=(Contact) getIntent().getSerializableExtra("afficher");

     et1=(EditText) this.findViewById(R.id.editText4);
    String  str1=c.getNomPrenom();

     et2=(EditText) this.findViewById(R.id.editText5);
    String  str2=c.getmPortable();

     et3=(EditText) this.findViewById(R.id.editText6);
    String  str3=c.getmFixe();

    et1.setText(str1);
    et2.setText(str2);
    et3.setText(str3);

    Button btn_retour=(Button) findViewById(R.id.button5);
    btn_retour.setOnClickListener(retour);
    Button btn_valider=(Button) findViewById(R.id.button6);
    btn_valider.setOnClickListener(valider);
}
private OnClickListener valider= new OnClickListener() {

    public void onClick(View v) {


        c.setNomPrenom(et1.getText().toString());
        c.setmPortable(et2.getText().toString());
        c.setmFixe(et3.getText().toString());

        i.putExtra(ContactActivity.TAG_AJOUT_EDIT, c);

        setResult(RESULT_OK,i);

        finish();
    }
};

private OnClickListener retour= new OnClickListener() {

    public void onClick(View v) {

        finish();
    }
};
Hosni
  • 668
  • 8
  • 29
  • please add the stacktrace/crash log. – Sam Dozor Feb 21 '12 at 19:39
  • @Sam_D i don't really know how to get it.i am still a newbie. is it the logCat? errors that you want?? thank you for answering – Hosni Feb 21 '12 at 19:52
  • May be this can be of help to you : http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – Ratan Feb 21 '12 at 19:54
  • @kool4u it's actually interesting but it doesn't help much with the On BackPressed. Thank you anyway. – Hosni Feb 21 '12 at 20:03
  • If you want to go to previous activity without passing any data then onClick of the button, just finish() the current activity ... it will go back to previous activity – Ratan Feb 21 '12 at 20:08
  • @kool4u i tried this one, it doesn't work. is there a big difference between using the onClickListener() and the android:onClick? – Hosni Feb 21 '12 at 20:11

1 Answers1

1

From the exception code you can see that there is a NullPointerException somewhere in onActivityResult. When handling an onClick event for the button valider, you call the method setResult(), but you don't when the button retour is clicked, and that's the problem, I guess.

m1ntf4n
  • 493
  • 2
  • 14