2012-02-21 219 views
0

我正試圖獲得一個安靜的簡單活動來工作。 我的活動包括兩個按鈕,我試圖讓他們中的一個作爲一個簡單的後退按鈕(回到上一個活動)提供任何幫助?onBackPressed()導致崩潰

這裏是我的代碼:

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(); 
} 

這裏是logcat的節目:

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) 

預先感謝您。

編輯:

我也試過,但它並沒有幫助:

 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(); 
    } 
}; 
+0

請添加堆棧跟蹤/崩潰日誌。 – 2012-02-21 19:39:54

+0

@Sam_D我真的不知道如何得到它。我仍然是一個新手。它是logCat?你想要的錯誤?謝謝你回答 – Hosni 2012-02-21 19:52:49

+0

可能是這對你有幫助: http://stackoverflow.com/questions/2497205/how-to-return-a-result-startactivityforresult-from-a-tabhost-activity – Ratan 2012-02-21 19:54:09

回答

1

從異常代碼,你可以看到有一個NullPointerException某處onActivityResult。在處理按鈕valider的onClick事件時,您可以調用setResult()方法,但是當點擊按鈕retour時不會發生這種情況,我猜這就是問題所在。