onBackPressed() 导致崩溃

发布于 2025-01-07 22:49:33 字数 4211 浏览 0 评论 0原文

我正在努力让一项安静简单的活动发挥作用。 我的活动包括两个按钮,我试图将其中一个按钮用作简单的后退按钮(返回到上一个活动)有什么帮助吗?

这是我的代码:

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

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

嘦怹 2025-01-14 22:49:33

从异常代码中可以看到onActivityResult中某处存在NullPointerException。当处理按钮验证器的 onClick 事件时,您调用方法 setResult(),但在单击按钮返回时则不调用方法,我猜这就是问题所在。

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文