gpt4 book ai didi

java - 如何将结果从 alertDialog 传递到 onActivityResult

转载 作者:行者123 更新时间:2023-11-30 03:14:17 24 4
gpt4 key购买 nike

我有 Activity_A 和 Activity_B。我使用 onActivityResult 但遇到问题:

java.lang.RuntimeException: Failure delivering result ResultInfo{who=null, request=3, result=-1, data=Intent { (has extras) }} to activity {com.example.sellcar/com.example.sellcar.View_Offer}: android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0

我想我不能通过这种方式将“结果”从 alertDialog 传递到 onActivityResult。我不知道如何解决这个问题:/请帮忙

Activity_A:

bBUTTON.setOnClickListener(new OnClickListener() {          
@Override
public void onClick(View v) {
Intent intent = new Intent(Activity_A.this,Activity_B.class);
startActivityForResult(intent, 3);

}
});
...
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == 3 && resultCode == RESULT_OK){

String pId = data.getStringExtra("MyData");

Toast.makeText(Activity_A.this,pId,Toast.LENGTH_LONG).show();
}
}

Activity _B:

    AlertDialog.Builder builder=new AlertDialog.Builder(View_Sell.this);
builder.setTitle("UWAGA !").setMessage("blablabla");

builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
String ostatnioDodanaSprzedaz="XYZ";
Intent intent = new Intent();
intent.putExtra("MyData", ostatnioDodanaSprzedaz);
setResult(RESULT_OK, intent);
onBackPressed();
} });

builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
} });

AlertDialog ad = builder.create();
ad.show();

最佳答案

尝试替换:

setResult(RESULT_OK, intent);
onBackPressed();

在:

if (getParent() == null) {
setResult(RESULT_OK, intent);
} else {
getParent().setResult(RESULT_OK, intent);
}
finish();

关于java - 如何将结果从 alertDialog 传递到 onActivityResult,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20410609/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com