我正在尝试将listview中的文本和图像转换为使用request_code的少量图像视图(按钮)和文本视图。此刻我有错误“java.lang.RuntimeException:将resultResultInfo {who = null,request = 1,result = -1,data = intent {(has extras)}}失败到活动......
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活动
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();
}
});
}
}
答案 0 :(得分:1)
if (resultCode == RESULT_OK) {
Intent intent = getIntent();
将以上代码替换为以下
if (resultCode == RESULT_OK) {
Intent intent = data;