The variable I am trying to send is instantiated at the top of the code and assigned its array values further down.
Intent subject_and_grades = new Intent(main_questionnaire.this, subject_and_grades.class); //if there are no errors display the next page
subject_and_grades.putExtra("user_inputs", user_inputs);
startActivity(subject_and_grades);
But in the next class the variable is always null.
Intent extras = getIntent();
String Student_Attributes = extras.getStringExtra("user_inputs");
here is the code of the first activity, the variable is first initialised right at the top of the code, then its values are added further down.
public class main_questionnaire extends AppCompatActivity {
String [] user_inputs;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_questionnaire);
getSupportActionBar().hide();
final TextView error_display = findViewById(R.id.error_display);
Button page_2 = findViewById(R.id.page_2);
page_2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String age = "17";
String Pstatus = "1"; //due to the fact i don't want to enter all the text fields every time i
String address = "1"; // test the code, i hardcoded in the attributes for testing.
String Medu = "4";
String Fedu = "4";
String traveltime = "1";
String studytime = "3";
String schoolsup = "0";
String activities = "1";
String higher = "1";
String absences = "4";
String qualification = "1";
//so we can iterate through all the user inputs
int lower_bound[] = {0, 0, 0, 15, 1, 1, 0, 0, 0, 0, 0, 0}; //validation range for each text field from 0
int upper_bound[] = {4, 4, 1, 22, 4, 4, 1, 1, 1, 93, 1, 1};
user_inputs = new String[]{Medu, Fedu, Pstatus, age, traveltime, studytime, schoolsup, address, activities, absences, higher, qualification};
//all the ids for all the warnings above the text views
String warning_ids[] = {"Text_View_Warning_1", "Text_View_Warning_2", "Text_View_Warning_3", "Text_View_Warning_4", "Text_View_Warning_5",
"Text_View_Warning_6", "Text_View_Warning_7", "Text_View_Warning_8", "Text_View_Warning_9", "Text_View_Warning_10",
"Text_View_Warning_11", "Text_View_Warning_12"};
//this loop iterates over all the answer and calls a function that validates them
for (int i = 0; i < user_inputs.length; i++) {
int error_writer_answer;
error_writer_answer = error_writer(warning_ids[i], user_inputs[i], lower_bound[i], upper_bound[i]);
if (error_writer_answer > 0) { //checks to see if the error counter has counted any errors
error_display.setText("Errors above: " + error_writer_answer); //print the ammount of errors the user has got at the bottom of the page
} else {
if (i == user_inputs.length-1) {
Intent subject_and_grades = new Intent(main_questionnaire.this, subject_and_grades.class); //if there are no errors display the next page
subject_and_grades.putExtra("user_inputs", user_inputs);
startActivity(subject_and_grades);
}
}
}
}
});
}
second page code
package com.example.smartrevisionschedule2;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.text.method.ScrollingMovementMethod;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import com.chaquo.python.PyObject;
import com.chaquo.python.Python;
import com.chaquo.python.android.AndroidPlatform;
public class subject_and_grades extends AppCompatActivity {
Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String[] array = bundle.getStringArray("user_inputs");
question from:
https://stackoverflow.com/questions/65904759/varible-is-always-null-when-using-android-studio-put-extra