java - 在 Android Studio 中从 Intent 接收数据时出现问题
问题描述
我正在编写通过 Intent 发送和接收数据的代码
但是,代码总是有问题:summaryResult
请问,谁能帮帮我?
发送数据的代码:
``` nextPage.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
Toast.makeText(getApplicationContext(), "It was sent: " + summaryResult, Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(), UserRegistration2.class);
startActivity(intent);
Bundle param = new Bundle();
param.putString("name", name);
param.putString("birthDate", birthDate);
parame.putString("city", city);
param.putString("summaryResult", summaryResult);
senderIntent.putExtras(param);
startActivity(senderIntent);
startActivity(intent);
}
});```
接收数据的代码:
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult =
receiverBundle.
getString
**("summaryResult");**
Toast.makeText(UserRegistration.this, "Receiving the Summary Result: " +summaryResult, Toast.LENGTH_LONG).show();```
解决方案
您声明了 startActivity 4 次,这是错误的,首先您应该将所有需要的数据放入包中,将包添加到意图中,然后只启动一次活动。代码应如下所示:
public void onClick(View view) {
Intent senderIntent = new Intent(getApplicationContext(), UserRegistration2.class);
Bundle sendlerBundler = new Bundle();
sendlerBlundler.putString("summaryResult", summaryResult);
senderIntent.putExtras(sendlerBundler);
startActivity(senderIntent);
}
接收活动中的代码:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.userRegistration);
Intent receiverIntent = getIntent();
Bundle receiverBundle = receiverIntent.getExtras();
String summaryResult = receiverBundle.getString("summaryResult");
}
推荐阅读
- scope - 如何访问范围之外的 rust 变量?
- directory - 如何使用目录文件夹重命名所选文件夹中的文件,并使用 Applescript 重命名所选文件夹
- ios - 以横向显示 Apple 登录视图?
- jenkins - Jenkins - 舞台重新运行的全局状态
- javascript - 如何将动态变量添加到数组中?
- sql - 使用 SQL Server 的列名无效
- android - 获取移动网络IP地址
- javascript - 我将使用什么 JavaScript 代码来绘制趋势线?
- postgresql - 从表中排序日期
- php - 我有 WordPress 网站我需要压缩并从一些帖子中下载一些图片