通过 Intent 从 ViewHolder 获取 Team Code
Get Team Code from ViewHolder through Intent
当我在 viewHolder 中点击联盟中的特定球队 Table 时,我正在尝试获取每支英超球队的球队代码。我已经成功地通过一个 activity 的意图将团队名称和团队位置发送给另一个。我已将团队代码硬编码到我的配置 Class
中的每个团队
我不确定如何解决这个错误。
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 方法,它适用于其他两个意图(teamName,position)
@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 Class
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)
错误提示您正在对空对象引用调用 Intent.getStringExtra(java.lang.String)
。查看出现错误的代码,
int teamCode = Integer.parseInt(getIntent().getStringExtra("teamCode"));
getStringExtra()
正在对 getIntent()
的 return 值调用。所以这意味着 getIntent()
是 returning null。
此调用发生在任何方法之外的字段初始值设定项中。这意味着它在 DetailActivity 的构造函数之前被调用(尽管在超类 Activity 的构造函数之后)。
更重要的是,getIntent()
's doc says "Return the intent that started this activity." But started can be a technical term in the activity's lifecycle。如果您查看生命周期图,您可以看到 starting 在 activity 的 creation 之后(即在Activity 对象的实例化)。一个 activity 可以 启动 ,甚至 创建 ,通过不同的意图多次,其对象没有被 实例化不止一次。 (第一次 started,它会首先被 created,这是必要的。)因此,虽然这并不明显,但[= =48=] 在 创建 之前可能不可用,即使超类 activity 已经 实例化 .
将此初始化代码移至 onCreate()
,您的大部分 activity 设置代码都应该放在该位置。然后你可以确信 activity 已经实例化,超类 onCreate()
已经完成,并且应该有 getIntent()
到 return.[=20= 的意图]
当我在 viewHolder 中点击联盟中的特定球队 Table 时,我正在尝试获取每支英超球队的球队代码。我已经成功地通过一个 activity 的意图将团队名称和团队位置发送给另一个。我已将团队代码硬编码到我的配置 Class
中的每个团队我不确定如何解决这个错误。
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 方法,它适用于其他两个意图(teamName,position)
@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 Class
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)
错误提示您正在对空对象引用调用 Intent.getStringExtra(java.lang.String)
。查看出现错误的代码,
int teamCode = Integer.parseInt(getIntent().getStringExtra("teamCode"));
getStringExtra()
正在对 getIntent()
的 return 值调用。所以这意味着 getIntent()
是 returning null。
此调用发生在任何方法之外的字段初始值设定项中。这意味着它在 DetailActivity 的构造函数之前被调用(尽管在超类 Activity 的构造函数之后)。
更重要的是,getIntent()
's doc says "Return the intent that started this activity." But started can be a technical term in the activity's lifecycle。如果您查看生命周期图,您可以看到 starting 在 activity 的 creation 之后(即在Activity 对象的实例化)。一个 activity 可以 启动 ,甚至 创建 ,通过不同的意图多次,其对象没有被 实例化不止一次。 (第一次 started,它会首先被 created,这是必要的。)因此,虽然这并不明显,但[= =48=] 在 创建 之前可能不可用,即使超类 activity 已经 实例化 .
将此初始化代码移至 onCreate()
,您的大部分 activity 设置代码都应该放在该位置。然后你可以确信 activity 已经实例化,超类 onCreate()
已经完成,并且应该有 getIntent()
到 return.[=20= 的意图]