我通过Intent传递一个int但是我不知道如何接收它因为我必须从OnCreate方法接收一个意图但是如果我把它放在那里我就不能将它与另一个int进行比较其余代码: 我在这里发送意图:
public class HomeActivityPro extends ActionBarActivity {
EditText conttext = (EditText) findViewById ( R.id.texthome );
Button buttone = (Button) findViewById(R.id.buttone);
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
View.OnClickListener maxim = new View.OnClickListener() {
@Override
public void onClick (View view) {
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
我收到它:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
Bundle bundle;
int maxnumpre = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
Intent wall = getIntent();
int maxnumpre = wall.getIntExtra("maxPressed", 0);}
但是在onCreate方法之后我必须这样做:
if (contone >= maxnumpre ){
resultaone.setText("You Failed" + " " + maxnumpre);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
答案 0 :(得分:0)
您需要在声明中获取onCreate
方法内传递的数据。
您还没有发送Bundle
,而是在String
发送了Intent
。因此,您需要从String
获取Intent
。
这样做
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
Bundle bundle;
String maxPressed = "";
int maxcont = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
maxPressed = getIntent().getStringExtra("maxNumberPressed");
try {
maxCont = Integer.parseInt(maxPressed);
}
catch (NumberFormatException e) {
e.printStackTrace();
}
}
//the rest of the code
还发送这样的数据
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxNumberPressed", conttext.getText().toString());
startActivity(wall);