首页 > 解决方案 > 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;
    }
}

标签: javaandroidandroid-intent

解决方案


您没有将intent数据发送回,MainActivity因为您尚未将“意图”值设置为setResult方法

代替

intent.putExtras(test2Bundle);
setResult(RESULT_OK);

intent.putExtras(test2Bundle);
setResult(RESULT_OK,intent);

推荐阅读