Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
163 views
in Technique[技术] by (71.8m points)

java - Varible is always null when using android studio put extra

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You should use get method appropriate your put.

If you put StringArray, you should use getStringArrayExtra() method.

String[] Student_Attributes = intent.getStringArrayExtra("user_inputs");

Please check your put type and get methods.

PS:

You can use Bundle for transfer data between two activities.

When put:

Bundle bundle = new Bundle();
bundle.putStringArray("user_inputs", user_inputs);
Intent intent=new Intent(main_questionnaire.this, subject_and_grades.class);
intent.putExtras(bundle);
startActivity(intent);

When get:

Intent intent = getIntent();
Bundle bundle = intent.getExtras();
String[] array = bundle.getStringArray("user_inputs");

PS

You should put getIntent() code in onCreate() method instead Activity class directly.

So you need like this.

public class subject_and_grades extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        ..............

        Intent intent = getIntent();
        Bundle bundle = intent.getExtras();
        String[] array = bundle.getStringArray("user_inputs");
    }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...