简体   繁体   中英

How to pass multiple edittext values into a Arraylist and then send it to another activity?

I am trying to pass 5 edittext values into a Arraylist and then send it to another activity. I use intent to send it to the next activity, but it keeps crashing. I get this error:

java.lang.NullPointerException: Attempt to read from null array

I think the way I add the values into the arraylist is wrong or something is wrong with sending the arraylist in the intent.

Activity 1:

EditText p1, p2, p3, p4, p5;
ArrayList<String> playerList = new ArrayList<>();
BootstrapButton btnStart;

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

    btnStart = (BootstrapButton) findViewById(R.id.start);
    p1 = (EditText) findViewById(R.id.player1);
    p2 = (EditText) findViewById(R.id.player2);
    p3 = (EditText) findViewById(R.id.player3);
    p4 = (EditText) findViewById(R.id.player4);
    p5 = (EditText) findViewById(R.id.player5);

    playerList.add(p1.getText().toString());
    playerList.add(p2.getText().toString());
    playerList.add(p3.getText().toString());
    playerList.add(p4.getText().toString());
    playerList.add(p5.getText().toString());

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

            Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
            myIntent.putStringArrayListExtra("player_list", playerList);
            MainActivity.this.startActivity(myIntent);
        }

    });
  }

Activity 2:

TextView playerName;
ArrayList<String> playerList = new ArrayList<>();

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    playerName = (TextView) findViewById(R.id.playerName);

    Intent myIntent = getIntent();
    if(myIntent != null){
        playerList = myIntent.getStringArrayListExtra("player_list");
        playerName.setText(playerList.get(0));
    }
  }

When I am using it like this with the hardcoded array values it doesn't crash and the value will be printed out correctly in the next activity.

Activity 1:

String[] array1={"asd","fgh","dcf","dg","ere","dsf"};
Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
myIntent.putExtra("player_list", array1);
MainActivity.this.startActivity(myIntent);

Activity 2:

    Intent myIntent = getIntent();
    if(myIntent != null){
        String[] players = myIntent.getStringArrayExtra("player_list");
        playerName.setText(players[2]);
    }else{
        System.out.println("Error!");
    }

What I want to do is having an Arraylist like this ["p1","p2","p3","p4","p5"] and then in the next activity retrieving the arraylist and printing out a random name from that array.

Use getStringArrayListExtra not getStringArrayExtra

if(myIntent != null){
        String[] players = myIntent.getStringArrayListExtra("player_list");
        playerName.setText(players[2]);
    }

Use intent.putStringArrayListExtra in Activity 1 to send and getStringArrayListExtra() in Activity 2 to retrieve the String ArrayList.

Here is the code to do so:

Activity 1:

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

    btnStart = (BootstrapButton) findViewById(R.id.start);
    p1 = (EditText) findViewById(R.id.player1);
    p2 = (EditText) findViewById(R.id.player2);
    p3 = (EditText) findViewById(R.id.player3);
    p4 = (EditText) findViewById(R.id.player4);
    p5 = (EditText) findViewById(R.id.player5);

    playerList.add(p1.getText().toString());
    playerList.add(p2.getText().toString());
    playerList.add(p3.getText().toString());
    playerList.add(p4.getText().toString());
    playerList.add(p5.getText().toString());

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

            Intent myIntent = new Intent(MainActivity.this, GameActivity.class);
            myIntent.putStringArrayListExtra("player_list", playerList);
            MainActivity.this.startActivity(myIntent);
        }

    });
  }

Activity 2:

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_game);
    setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);

    playerName = (TextView) findViewById(R.id.playerName);

    Intent myIntent = getIntent();
    if(myIntent != null){
            ArrayList<String> players = myIntent.getStringArrayListExtra("player_list");
            playerName.setText(players.get(0));
        }else{
            System.out.println("Error!");
        }
      }

Also make sure playerList is declared and initialized. Your code does not show it being declared nor initialized.

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