onBackPressed()导致崩溃

时间:2012-02-21 19:33:45

标签: android android-activity back

我想尝试一个安静的简单活动。  我的活动包括两个按钮,我试图使其中一个作为一个简单的后退按钮(回到以前的活动)任何帮助吗?

这是我的代码:

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

1 个答案:

答案 0 :(得分:1)

从异常代码中可以看到onActivityResult中的某处存在NullPointerException。当处理按钮valider的onClick事件时,你调用方法setResult(),但是当你点击按钮retour时你没有,这就是问题,我想。