Moving data in Android Studio Between Activities-ThrowExceptions

Exception or error:

I’m trying to code an app in Android Studio that has two activities. How do I move the data from PlainTextSubject1 in Activity 2 to TextViewSubject1 in Main Activity after a button is clicked? I feel like I have been all over the place with figuring this out.

MainActivity.java Code:

package com.example.studentgrades;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {
    private Button button;
    TextView receive;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);


        button = findViewById(R.id.button);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                nextPage();
            }
        });
    }
    public void nextPage(){
        Intent intent = new Intent(this, Main2Activity.class);
        startActivity(intent);
    }
}

Main2Activity.java Code:

package com.example.studentgrades;

import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import javax.security.auth.Subject;

public class Main2Activity extends AppCompatActivity {
    Button button3;
    EditText subject1, subject2, subject3;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);

        subject1 = (EditText) findViewById(R.id.edittextsubject1);
        subject2 = (EditText) findViewById(R.id.edittextsubject2);
        subject3 = (EditText) findViewById(R.id.edittextsubject3);

        button3 = (Button) findViewById(R.id.button3);

        button3.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                String subject1value = subject1.getText().toString();
                String subject2value = subject2.getText().toString();
                String subject3value = subject3.getText().toString();
                backPage();

                );
            }
        });
    }
    public void backPage(){
        Intent intent = new Intent(this, MainActivity.class);
        startActivity(intent);
    }
}
How to solve:

As ryan said, you would use an intent:

Intent intent = new Intent(getBaseContext(), SignoutActivity.class);
intent.putExtra("TextView content 1", textView.getText().toString());
startActivity(intent);

Then on your second activity you can do this:

TextView textView2 = findViewById(whatever your id is) 
textView2.setText(getIntent.getStringExtra("TextView content 1"));

Leave a Reply

Your email address will not be published. Required fields are marked *