java - startActivityForResult 发送数据,但返回的 Intent 没有数据
问题描述
我正在尝试将数据传递给 TestActivity,然后在完成后返回新数据。
基本上,MainActivity 将数据放入 Intent 并启动 ActivityForResult。此 TestActivity 在 TextView 中显示数据并单击按钮将新数据添加到新 Intent 并将用户发送回 MainActivity。回到 MainActivity,onActivityResult 执行应该包含新数据。
Intent 中包含的数据可以很好地传递给 TestActivity,但我似乎找不到返回到 MainActivity 的新数据。我不断收到 NullPointException:
java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()”。
谢谢您的帮助!
这是主要活动:
// Define the StartActivityForResult and the ActivityResultCallback
ActivityResultContracts.StartActivityForResult testStartActivityForResult = new ActivityResultContracts.StartActivityForResult();
ActivityResultCallback<ActivityResult> testActivityResultCallback = new ActivityResultCallback<ActivityResult>() {
// This code executes after the testActivity is complete
@Override
public void onActivityResult(ActivityResult result) {
if (result.getResultCode() == Activity.RESULT_OK) {
Intent test2Intent = result.getData(); // Empty.
//Intent test2Intent = getIntent(); // I tried getIntent, but that is also empty.
Bundle test2Bundle = test2Intent.getExtras(); //NullPointerException
User test2User = (User) test2Bundle.getSerializable(USER_KEY); //
Toast.makeText(MainActivity.this, test2User.getUsername(), Toast.LENGTH_SHORT).show();
}
}
};
// Here I create my testLauncher and fill a Bundle with data
ActivityResultLauncher<Intent> testLauncher = registerForActivityResult(testStartActivityForResult, testActivityResultCallback);
Bundle testBundle = new Bundle();
testBundle.putSerializable(USER_KEY, new User("TestUserName"));
Intent testIntent = new Intent(this, TestActivity.class);
testIntent.putExtras(testBundle);
// Launch the testActivity
testLauncher.launch(testIntent);
这是测试活动:
public class TestActivity extends AppCompatActivity {
TextView testTextView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_test);
Button testButton = findViewById(R.id.testButton);
testTextView = findViewById(R.id.testTextView);
Intent intent = getIntent();
User testUser = (User)intent.getSerializableExtra(USER_KEY);
testTextView.setText(testUser.getUsername());
testButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
User test2User = new User("test2Username");
Bundle test2Bundle = new Bundle();
test2Bundle.putSerializable(USER_KEY, test2User);
intent.putExtras(test2Bundle);
setResult(RESULT_OK);
finish();
}
});
}
}
这是我的自定义对象的简化,序列化以通过 Intents:
public class User implements Serializable {
private String username;
public User(String username) {
this.username = username;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
}
解决方案
您没有将intent
数据发送回,MainActivity
因为您尚未将“意图”值设置为setResult
方法
代替
intent.putExtras(test2Bundle);
setResult(RESULT_OK);
和
intent.putExtras(test2Bundle);
setResult(RESULT_OK,intent);
推荐阅读
- docker - 使用 HTTP 公开 API 时 Docker 拉取失败
- google-cloud-storage - 为什么当我创建谷歌存储桶时,它会提醒我存在 buck 名称?
- mysql - MySQL ORDER BY 不按字母顺序排序
- wordpress - 如何阻止 Wordpress 在保存后更改我的链接 href 属性
- django - 无法创建唯一索引,键重复 django postgres
- r - 如何有条件地合并/复制不同的行[R]
- google-colaboratory - 在 Google Colab 上训练模型时,我应该保持连接吗?
- r - fviz_mclust 中的轴定义
- cypress - Cypress - 如何在方法 CONTAINS 中按顺序获取第三个元素?
- javascript - 如何在 django 模板中设置两个 for 循环来发送和接收聊天?