Android Question

How to get text and img from Listview to another activity using requestcode

I'm trying to get text and images from listview to few imageviews(buttons) and textviews that use request_code. At this moment I have error "java.lang.RuntimeException:Failure delivering resultResultInfo{who=null, request=1, result=-1, data=intent{(has extras)}} to activity...

MainActivity

public class MainActivity extends AppCompatActivity implements
View.OnClickListener{

final int REQUEST_CODE_PLAYER1 = 1;
final int REQUEST_CODE_PLAYER2 = 2;
int position;
String[] name;
int[] photo;
TextView playerName1;
TextView playerName2;

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

playerName1 = (TextView)findViewById(R.id.playerName1);
playerName2 = (TextView)findViewById(R.id.playerName2);
btnPlayer1 = (ImageView)findViewById(R.id.imgPlayer1);
btnPlayer2 = (ImageView)findViewById(R.id.imgPlayer2);
btnPlayer1.setOnClickListener(this);
btnPlayer2.setOnClickListener(this);

public void onClick(View v) {
Intent intent;
switch (v.getId()) {
case R.id.imgPlayer1:
intent = new Intent(this, ListHeroes.class);
startActivityForResult(intent,REQUEST_CODE_PLAYER1);
break;
case R.id.imgPlayer2:
intent = new Intent(this, ListHeroes.class);
startActivityForResult(intent,REQUEST_CODE_PLAYER2);
break;
default:
break;
}
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK) {
Intent intent = getIntent();
switch (requestCode) {
case REQUEST_CODE_PLAYER1:
position = intent.getExtras().getInt("position");
name = intent.getStringArrayExtra("name");
photo = intent.getIntArrayExtra("photo");
playerName1.setText(name[position]);
btnPlayer1.setImageResource(photo[position]);
break;

case REQUEST_CODE_PLAYER2:
position = intent.getExtras().getInt("position");
name = intent.getStringArrayExtra("name");
photo = intent.getIntArrayExtra("photo");
playerName2.setText(name[position]);
btnPlayer2.setImageResource(photo[position]);
break;

}
} else {
Toast.makeText(this, "Hero is not selected",
Toast.LENGTH_SHORT).show();
}

}
}


ListHeroes Activity

public class ListHeroes extends Activity {

ListView list;
ListViewAdapter adapter;
String[] name;
int[] photo;

public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.listview_main);

name = new String[]{"kamen", "nozhnitsi", "bumaga"};
photo = new int[]{R.drawable.kamen, R.drawable.nozhnitsi, R.drawable.bumaga}

list = (ListView) findViewById(R.id.listview);

// Pass results to ListViewAdapter Class
adapter = new ListViewAdapter(this, name, photo);

// Binds the Adapter to the ListView
list.setAdapter(adapter);

// Capture ListView item click
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent();

intent.putExtra("name", name);

intent.putExtra("photo", photo);

intent.putExtra("position", position);

setResult(RESULT_OK, intent);
finish();

}

});
}
}

Answer Source
 if (resultCode == RESULT_OK) {
        Intent intent = getIntent();  

Replace above code with following

if (resultCode == RESULT_OK) {
        Intent intent = data;