【发布时间】:2017-05-31 03:19:31
【问题描述】:
当我在 viewHolder 中点击联赛表中的特定球队时,我试图获取每支英超球队的球队代码。我已经成功地能够通过从一项活动到另一项活动的意图发送团队名称和团队位置。我已将团队代码硬编码到我的配置类中的每个团队
我不确定如何解决这个错误。
public static int getCodeFromName(String teamname) {
switch (teamname) {
case "Arsenal FC":
return 57;
case "AFC Bournemouth":
return 1044;
case "Burnley FC":
return 328;
case "Chelsea FC":
return 61;
case "Crystal Palace FC":
return 354;
case "Hull City FC":
return 322;
case "Liverpool FC":
return 164;
case "Manchester City FC":
return 65;
case "Manchester United FC":
return 66;
case "Middlesbrough FC":
return 343;
case "Southampton FC":
return 340;
case "Swansea City":
return 72;
case "Leicester City FC":
return 338;
case "Everton FC":
return 62;
case "West Ham United FC":
return 563;
case "Tottenham Hotspur FC":
return 73;
case "Watford FC":
return 346;
case "West Bromwich Albion FC":
return 74;
case "Sunderland AFC":
return 71;
case "Stoke City FC":
return 70;
default:
return 0;
}
}
下面是我的 OnClick 方法,它适用于其他两个意图(团队名称、位置)
@Override
public void onClick(View view) {
int teamCode = Configs.getCodeFromName(teamNameTxt.getText().toString());
int position = getLayoutPosition(); // gets item position
Intent intent = new Intent(view.getContext(), DetailActivity.class);
intent.putExtra("teamName", teamNameTxt.getText().toString());
intent.putExtra("position", teamPositionTxt.getText().toString());
intent.putExtra("teamCode", teamCode);
view.getContext().startActivity(intent);
最后,在我的 DetailActivity 类中
int teamCode = Integer.parseInt(getIntent().getStringExtra("teamCode")); //ERROR OCCURS HERE
String PlayersURL = "http://api.football-data.org/v1/teams/" + teamCode + "/players";
final String teamName = getIntent().getStringExtra("teamName");
String teamPosition = getIntent().getStringExtra("position");
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.team_overview);
rv = (RecyclerView) findViewById(R.id.rv);
if (rv != null) {
rv.setLayoutManager(new LinearLayoutManager(this));
}
teamNameTV = (TextView) findViewById(R.id.teamNameTV);
teamNameTV.setText("Name: " + teamName);
teamPositionTV = (TextView) findViewById(R.id.teamPositionTV);
teamPositionTV.setText("Position : " + teamPosition);
button_players = (Button) findViewById(R.id.button_players);
button_players.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new PlayerDataDownloader(DetailActivity.this, PlayersURL, rv).execute();
}
});
错误代码:
Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'java.lang.String android.content.Intent.getStringExtra(java.lang.String)' on a null object reference
at com.example.oisin.premierleaguesocial.activities.DetailActivity.<init>(DetailActivity.java:26)
【问题讨论】:
标签: android android-intent android-viewholder