如何将多个edittext值传递给Arraylist然后将其发送到另一个活动?

时间:2017-07-12 22:40:42

标签: java android arrays

我正在尝试将5个edittext值传递给Arraylist,然后将其发送到另一个活动。我使用intent将它发送到下一个活动,但它一直在崩溃。我收到这个错误:

  

java.lang.NullPointerException:尝试从空数组中读取

我认为将值添加到arraylist中的方式是错误的,或者在意图中发送arraylist时出现了问题。

活动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);
        }

    });
  }

活动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));
    }
  }

当我像这样使用硬编码数组值时,它不会崩溃,并且值将在下一个活动中正确打印出来。

活动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);

活动2:

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

我想做的是拥有Arraylist这样的["p1","p2","p3","p4","p5"],然后在下一个活动中检索arraylist并从该数组中打印出一个随机名称。

2 个答案:

答案 0 :(得分:2)

使用getStringArrayListExtra而不是getStringArrayExtra

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

答案 1 :(得分:1)

使用活动1中的intent.putStringArrayListExtra发送活动2中的getStringArrayListExtra()以检索String ArrayList。

以下是执行此操作的代码:

活动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);
        }

    });
  }

活动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!");
        }
      }

还要确保声明并初始化了playerList。您的代码未显示已声明或初始化。