我是java编程的新手,我正在尝试将信息传递给另一个类并将其显示为TextView。我有两个类:Match和MatchResult。在匹配中你可以按一个按钮,一个int将+1。我在网站上也有一个按钮,可以让你去下一堂课。在下一课我想要让分数显示在TextView中。但有些东西不起作用,我不明白为什么。这是我的代码希望有人可以帮助我:
Match.java:
public class Match extends Activity implements OnClickListener{
public final static String EXTRA_MESSAGE = "com.epstudios.basketballmanager_v1.MATCHRESULT";
TextView awayGoals, homeGoals, home, away;
Button homebtn, awaybtn, gotoresult;
int homecount, awaycount;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.match);
baconAndEggs();
homebtn.setOnClickListener(this);
awaybtn.setOnClickListener(this);
gotoresult.setOnClickListener(this);
}
public void sendInfo(View view) {
Intent intent = new Intent(this, Match.class);
intent.putExtra(EXTRA_MESSAGE, awaycount);
Intent homeintent = new Intent(this, Match.class);
homeintent.putExtra(EXTRA_MESSAGE, homecount);
}
private void baconAndEggs() {
awayGoals = (TextView) findViewById(R.id.Away);
homeGoals = (TextView) findViewById(R.id.Home);
homebtn = (Button) findViewById(R.id.homeBtn);
awaybtn = (Button) findViewById(R.id.awayBtn);
gotoresult = (Button) findViewById(R.id.matchResult);
}
public void counter() {
awaycount = 1;
homecount = 0;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.homeBtn:
homecount ++;
homeGoals.setText("Lakers: " + homecount);
break;
case R.id.awayBtn:
awaycount ++;
awayGoals.setText("Heat: " + awaycount);
break;
case R.id.matchResult:
Intent result = new Intent(this, MatchResult.class);
startActivity(result);
break;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
MatchResult.java:
public class MatchResult extends Activity {
TextView home, away;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.match_result);
resultcounter();
Intent awayintent = getIntent();
String awayresult = awayintent.getStringExtra(Match.EXTRA_MESSAGE);
Intent homeintent = getIntent();
String homeresult = homeintent.getStringExtra(Match.EXTRA_MESSAGE);
home.setText(homeresult);
Log.d("Petter", homeresult);
}
日志不起作用
答案 0 :(得分:0)
你添加为'Integer'putExtra(“KEY”,int)并检索为'String'(getStringExtra)
使用以下命令更改sendInfo:
public void sendInfo(View view) {
Intent intent = new Intent(this, MatchResult.class);
intent.putExtra(EXTRA_MESSAGE_AWAY, awaycount);
intent.putExtra(EXTRA_MESSAGE_HOME, homecount);
startActivituy(intent);
}
将MatchResult更改为:
public class MatchResult extends Activity {
TextView home, away;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.match_result);
resultcounter();
int awayresult = getIntent().getIntExtra(Match.EXTRA_MESSAGE_AWAY, -1);
int homeresult = getIntent().getIntExtra(Match.EXTRA_MESSAGE_HOME, -1);
home.setText(String.valueOf(homeresult));
Log.d("Petter", String.valueOf(homeresult));
}
答案 1 :(得分:0)
您正在使用此功能开始第二个活动:
case R.id.matchResult:
Intent result = new Intent(this, MatchResult.class);
result.putExtra(EXTRA_MESSAGE_HOME, awaycount);//Use this
result.putExtra(EXTRA_MESSAGE_AWAY, homecount);//Use this
startActivity(result);
break;
在这里放置一个意图。不要使用2个意图。只需将两个变量放在同一意图中使用2个键即可。
在第二项活动中这样做:
Intent intent = getIntent(); String awayresult = String.valueOf(intent.getIntExtra(Match.EXTRA_MESSAGE_AWAY,-1)); String homeresult = String.valueOf(intent.getIntExtra(Match.EXTRA_MESSAGE_HOME,-1));
答案 2 :(得分:0)
匹配度:
public class Match extends Activity implements OnClickListener {
public final static String EXTRA_MESSAGE_HOME = "com.epstudios.basketballmanager_v1.MATCHRESULT";
public final static String EXTRA_MESSAGE_AWAY = "com.epstudios.basketballmanager_v1.MATCHRESULT";
TextView awayGoals, homeGoals, home, away;
Button homebtn, awaybtn, gotoresult;
int homecount, awaycount;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.match);
baconAndEggs();
homebtn.setOnClickListener(this);
awaybtn.setOnClickListener(this);
gotoresult.setOnClickListener(this);
}
public void sendInfo(View view) {
Intent intent = new Intent(this, MatchResult.class);
intent.putExtra(EXTRA_MESSAGE_AWAY, awaycount);
intent.putExtra(EXTRA_MESSAGE_HOME, homecount);
startActivity(intent);
}
private void baconAndEggs() {
awayGoals = (TextView) findViewById(R.id.Away);
homeGoals = (TextView) findViewById(R.id.Home);
homebtn = (Button) findViewById(R.id.homeBtn);
awaybtn = (Button) findViewById(R.id.awayBtn);
gotoresult = (Button) findViewById(R.id.matchResult);
}
public void counter() {
awaycount = 1;
homecount = 0;
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.homeBtn:
homecount++;
homeGoals.setText("Lakers: " + homecount);
break;
case R.id.awayBtn:
awaycount++;
awayGoals.setText("Heat: " + awaycount);
break;
case R.id.matchResult:
Intent result = new Intent(this, MatchResult.class);
startActivity(result);
break;
}
}
@Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
}
MatchResult的:
public class MatchResult extends Activity {
TextView home, away;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.match_result);
resultcounter();
int awayresult = getIntent().getIntExtra(Match.EXTRA_MESSAGE_AWAY, -1);
int homeresult = getIntent().getIntExtra(Match.EXTRA_MESSAGE_HOME, -1);
home.setText(String.valueOf(homeresult));
Log.d("Petter", String.valueOf(homeresult));
away.setText(String.valueOf(awayresult));
}
public void resultcounter() {
// TODO Auto-generated method stub
home = (TextView) findViewById(R.id.homeresult);
away = (TextView) findViewById(R.id.awayresult);
}
}