android - Android Intent 数据返回 null
问题描述
我是 Android 的初学者,现在我想使用意图将数据从一个活动传输到另一个活动。第一个活动将收集数据并将其提供给第二个活动。但第二次数据返回空值。所以那里有一些错误。这是我的代码。
public void previewStack(final Context context, final CreateStackNewActivity createStackNewActivity, final Stack stackDetails) {
Map<String, String> options = new HashMap<String, String>();
if(stackDetails.getStackId() != -1){
options.put("stack_id", String.valueOf(stackDetails.getStackId()));
}
int index = 1;
for(StackLineNew stackLine : stackDetails.getLines()){
options.put("title" + index, stackLine.getTitle());
options.put("line_type" + index, Integer.toString(stackLine.getLineType().intValue));
options.put("title" + index, stackLine.getTitle());
if (stackLine.getBody().indexOf("file:/") == -1 && stackLine.getBody().indexOf("content:") == -1) {
options.put("description" + index, stackLine.getBody());
if (stackLine.getLineId() != -1) {
options.put("line_id" + index, Integer.toString(stackLine.getLineId()));
}
}
index++;
}
Call<GenericAPIResponse> call = Hype4DAPI.previewStack(stackDetails.getName(), stackDetails.getCategory(), Integer.toString(stackDetails.getLines().size()), options, "");
call.enqueue(new Callback<GenericAPIResponse>() {
@Override
public void onResponse(Call<GenericAPIResponse> call, Response<GenericAPIResponse> response) {
if(response.isSuccessful()) {
GenericAPIResponse saveStackResponse = response.body();
System.out.println(saveStackResponse);
Toast.makeText(context, saveStackResponse.toString(), Toast.LENGTH_SHORT).show();
createStackNewActivity.saveInProgress = false;
createStackNewActivity.somethingHasChanged = false;
createStackNewActivity.updatePostButtonState();
Intent itnt = new Intent(createStackNewActivity, StackDetailsPage.class);
itnt.putExtra("stackId", stackDetails.getStackId());
itnt.putExtra("isPreview", "true");
itnt.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
createStackNewActivity.startActivity(itnt);
} else {
System.out.println(response.errorBody());
Toast.makeText(context, response.errorBody().toString(), Toast.LENGTH_SHORT).show();
}
}
@Override
public void onFailure(Call<GenericAPIResponse> call, Throwable t) {
t.printStackTrace();
}
});
}
以及第二个活动的代码
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_stack_details);
final Intent intent = getIntent();
final String stackId;
if (intent != null) { //Null Checking
stackId = intent.getStringExtra("stackId");
isPreview = intent.getStringExtra("isPreview");
stackTitleStr = intent.getStringExtra("stackTitle");
saveLine = (HashMap<String, Object>)intent.getSerializableExtra("saveLine");
}
else{
stackId = null;
}
在第二个活动中,我所有变量都为空。isPreview、stackTitleStr、saveLine 都为空。所以任何人请尽快帮助我。
解决方案
首先,您不应该在 onResponse 回调方法中创建意图,而是在 StackDetailsPage 活动中创建一个静态方法,该方法返回它需要的意图。有了这个,这个活动“告诉”其他人它需要什么,它避免了重复的字符串额外键。用这个 :
//in StackDetailsPage activity
public static Intent getIntent(Context context, String stackId, String...)
{
Intent intent = new Intent(context, StackDetailsPage.class);
intent.putExtra("stackId", stackId);
...
return intent;
}
然后在 onResponse 方法中这样做:
startActivity(StackDetailsPage.getIntent(context,stackDetails.getStackId(), ...))
推荐阅读
- reactjs - 使用打字稿中的 react-final-form FieldRenderProps 扩展接口时发生冲突
- csv - 如何在 Neo4j 中使用带有授权和不记名令牌的“LOAD CSV FROM”命令?
- angular - 从 Angular 中的解析器返回 Observable 的目的
- python - keras CNN 的未知 OSERROR -2
- mongodb - 使用 mongodb-source-connect 时出现“仅在副本集上支持 $changeStream 阶段”错误
- python - 为什么我的 while 循环不能对所有值求和
- asp.net-mvc - Azure AD B2C 授权后返回 404 错误
- excel - 遍历数组时,有没有办法避免 EXCEL VBA 中出现类型不匹配错误?
- python - 根据多个条件更新熊猫数据框中的值
- python - 在 Mark Lutz 的“Learning Python”中混淆了 global 的使用