【问题标题】:startActivityForResult sends data, but the returned Intent contains no datastartActivityForResult 发送数据,但返回的 Intent 没有数据
【发布时间】:2021-10-09 09:53:11
【问题描述】:

我正在尝试将数据传递给 TestActivity,然后在完成后返回新数据。

基本上,MainActivity 将数据放入 Intent 并启动 ActivityForResult。此 TestActivity 在 TextView 中显示数据并单击按钮将新数据添加到新 Intent 并将用户发送回 MainActivity。 回到 MainActivity,onActivityResult 执行应该包含新数据。

Intent 中包含的数据可以很好地传递给 TestActivity,但我似乎无法找到返回到 MainActivity 的新数据。我不断收到 NullPointException:

java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法“android.os.Bundle android.content.Intent.getExtras()”。

感谢您的帮助!

这里是 MainActivity:

// 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;
    }
}

【问题讨论】:

    标签: java android android-intent


    【解决方案1】:

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

    替换

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

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

    【讨论】:

      猜你喜欢
      • 2016-12-22
      • 2017-05-08
      • 1970-01-01
      • 1970-01-01
      • 2022-11-01
      • 1970-01-01
      • 2012-11-24
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多