首页 > 解决方案 > 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 都为空。所以任何人请尽快帮助我。

标签: androidandroid-intent

解决方案


首先,您不应该在 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(), ...))


推荐阅读