gpt4 book ai didi

android - Bundel.getString ("") 在 onActivityResult() 给出空值

转载 作者:行者123 更新时间:2023-11-29 20:43:46 25 4
gpt4 key购买 nike

我正在开发 android 应用程序,在该应用程序中,我将数据从 Activity 2 传回 Activity 1。我正在通过 Intent 传递它。问题是在 Activity 1 中,它显示的是 bundle 中的值,但是当我使用 bundle.getString(""); 时,它显示的是 null 值。在调试中,我正在使用 bundle Bundle[{list_size=3}]。我的代码如下,请帮帮我。

Activity 1:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

if(resultCode==105){
TextView txtCounter = (TextView) findViewById(R.id.txtCounter);
// It showing value at MBuddle, but at MBuddle it is showing null.
Bundle MBuddle = data.getExtras();
String MMessage = MBuddle .getString("list_size");
txtCounter.setText(MMessage);
}
}

Activity 2:

@Override
public void onBackPressed() {
int REQUEST_CONTACTS = 105;
Intent intent = new Intent();
intent.putExtra("list_size", fListContacts.size());
setResult(REQUEST_CONTACTS, intent);
finish();
}

最佳答案

  @Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data)

if (requestCode == REQUEST_CODE) {
if (resultCode == Activity.RESULT_OK) {
String contents = intent.getStringExtra(Intents.Scan.RESULT);
}}
}

===============

            Intent intent = new Intent(getIntent().getAction());
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
intent.putExtra(KEY, data);
setResult(RESULT_OK, intent);
finish();

RESULT_OK 可以是一个 int 值

关于android - Bundel.getString ("") 在 onActivityResult() 给出空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30639324/

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