繁体   English   中英

从 Android Studio 中的 Intent 接收数据时出现问题

[英]Problem receiving data from an Intent in Android Studio

我正在编写通过 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");

}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM