App in Android Studio keeps crashing upon clicking submit when trying to share data using Intent and onClickListener

33 views Asked by At

The app is a general profile app to get familiar with the program since this is my first time using it and I don't have much experience with Java. The main activity has two radio buttons, a radio group, six checkboxes, five textviews (three labels and two to show results) and three edittexts. When you click apply the results from the radio buttons and the checkboxes will show a textview assigned to them. Clicking submit is supposed to move to the next screen that shows the information typed into the EditTexts and selected with the checkboxes and radiobuttons.

The Main activity java file:

public class MainActivity extends AppCompatActivity {
    @SuppressLint("SetTextI18n")
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        applyButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view) {
                if (cookingbox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + cookingbox.getText().toString());
                }
                if (sportsbox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + sportsbox.getText().toString());
                }
                if (craftbox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + craftbox.getText().toString());
                }
                if (gamebox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + gamebox.getText().toString());
                }
                if (bookbox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + bookbox.getText().toString());
                }
                if (otherbox.isChecked()) {
                    selectedhobby.setText(selectedhobby.getText().toString() + " " + otherbox.getText().toString());
                }
                if (malebutton.isChecked()) {
                    gendertext.setText(malebutton.getText().toString());
                } else if (femalebutton.isChecked()) {
                    gendertext.setText(femalebutton.getText().toString());
                }
            }
        });
        submitButton.setOnClickListener(new View.OnClickListener()
        {
            @Override
            public void onClick(View view)
            {
                Intent Intent;
                Intent = new Intent(MainActivity.this, Screen2Activity.class);
                Intent.putExtra("selectedhobby", "");
                Intent.putExtra("editTextText2", "");
                Intent.putExtra("editTextText4", "");
                Intent.putExtra("editTextText3", "");
                Intent.putExtra("gendertext", "");
                startActivity(Intent);
                setContentView(R.layout.screen2);
            }
        });
    }
}

Screen2 Java File:

import androidx.appcompat.app.AppCompatActivity;

public class Screen2Activity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.screen2);
        ;
        textName = findViewById(R.id.textName);
        textAge = findViewById(R.id.textAge);
        textBirthplace = findViewById(R.id.textBirthplace);
        textHobbies = findViewById(R.id.textHobbies);
        textGender = findViewById(R.id.textGender);

        Intent Intent = getIntent();
        if (Intent != null)
        {
            String editTextText2 = Intent.getStringExtra("editTextText2");
            String editTextText4 = Intent.getStringExtra("editTextText4");
            String selectedhobby = Intent.getStringExtra("selectedhobby");
            String gendertext = Intent.getStringExtra("gendertext");
            Integer editTextText3 = Intent.getIntExtra("editTextText3", -1);
            textName.setText(Log.d("Screen2Activity", "Name :  + editTextText2"));
            textAge.setText(Log.d("Screen2Activity", "Age :  + editTextText3"));
            textBirthplace.setText(Log.d("Screen2Activity", "Birthplace :  + editTextText4"));
            textHobbies.setText(Log.d("Screen2Activity", "Hobbies :  + selectedhobby"));
            textGender.setText(Log.d("Screen2Activity", "Gender :  + gendertext"));
        }

I couldn't get the information from the EditText fields to move to the second activity screen. When I went back into the main activity java file to try to fix the intent errors, I guess I messed up the code for the submit button as well and now neither will work.

1

There are 1 answers

1
CommonsWare On

Start by replacing:

            textName.setText(Log.d("Screen2Activity", "Name :  + editTextText2"));
            textAge.setText(Log.d("Screen2Activity", "Age :  + editTextText3"));
            textBirthplace.setText(Log.d("Screen2Activity", "Birthplace :  + editTextText4"));
            textHobbies.setText(Log.d("Screen2Activity", "Hobbies :  + selectedhobby"));
            textGender.setText(Log.d("Screen2Activity", "Gender :  + gendertext"));

with:

            Log.d("Screen2Activity", "Name :  + editTextText2");
            Log.d("Screen2Activity", "Age :  + editTextText3");
            Log.d("Screen2Activity", "Birthplace :  + editTextText4");
            Log.d("Screen2Activity", "Hobbies :  + selectedhobby");
            Log.d("Screen2Activity", "Gender :  + gendertext");

Do not attempt to use the output of Log.d(). Just look in Logcat to see what the Log.d() call emits in the logging.