简体   繁体   中英

Problem receiving data from an Intent in Android Studio

I'm writing code that sends and receives data through Intent

However, the code is always having problems in: summaryResult

Please, can anyone help me?

Code to send data:

    ``` 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);
        }
    });``` 

Code to receive data:

  @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();```

you declared startActivity 4 times which is wrong , first you should put all your desired data in the bundle , add the bundle to the intent then start the activity only once. the code should look like this:

 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);

    }

code in receiving activity:

   protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.userRegistration);

    Intent receiverIntent = getIntent();
    Bundle receiverBundle = receiverIntent.getExtras();
    String summaryResult = receiverBundle.getString("summaryResult");

}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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