将信息从类发送到另一个类getIntent

时间:2014-08-18 18:54:39

标签: java android xml eclipse android-intent

我一直在努力学习java,所以我可以为android编程。我创建了一个有2个类的应用程序,Match和MatchResult。在匹配中,您可以按一个按钮,它将向计数器添加一个按钮。在页面的底部,我有一个按钮,可以将您带到下一个课程并在textview中显示计数器。我试图通过getIntent发送信息,但我找不到它的错误。这是代码:

匹配度:

    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);
}
}

4 个答案:

答案 0 :(得分:0)

嗯,为什么不尝试废弃你的sendInfo()功能并在结果意图下添加此行:

result.putExtra(Match.EXTRA_MESSAGE_AWAY, awayCount);

现在应该可以了。

答案 1 :(得分:0)

似乎有两种方法调用MatchResult活动的代码因此尝试使用:

hasExtra()方法检查是否通过Intent传递了额外的内容。

答案 2 :(得分:0)

Intent的构造函数应该具有您的类的上下文。但在您的情况下,this指的是View.OnClickListener()!所以,只需改变它,如下所示。

...
case R.id.matchResult:
  Intent result = new Intent(Match.this, MatchResult.class); // Like here
  result.putExtra(EXTRA_MESSAGE_AWAY, awaycount);
  result.putExtra(EXTRA_MESSAGE_HOME, homecount);
  startActivity(result);
  break;
}
...

编辑:您从未使用过sendInfo方法。据我所知,你可以删除你的sendInfo方法并将这些代码放入switch case。

答案 3 :(得分:0)

我将创建一个在活动之间共享的静态变量。