Java 如何通过Intent接收int
我正在通过一个Intent传递一个int,但我不知道如何接收它,因为我必须从OnCreate方法接收Intent,但是如果我将它放在那里,我就无法将它与代码其余部分中的另一个int进行比较: 在这里,我要传达以下意图:Java 如何通过Intent接收int,java,android,android-intent,Java,Android,Android Intent,我正在通过一个Intent传递一个int,但我不知道如何接收它,因为我必须从OnCreate方法接收Intent,但是如果我将它放在那里,我就无法将它与代码其余部分中的另一个int进行比较: 在这里,我要传达以下意图: public class HomeActivityPro extends ActionBarActivity { EditText conttext = (EditText) findViewById ( R.id.texthome ); Button buttone = (Bu
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();
}
您需要获取在声明中没有的
onCreate
方法中传递的数据
另外,您没有发送捆绑包
,而是在意图
中发送字符串
。因此,您需要从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);
也可以像这样发送数据
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);
应用程序正在崩溃,但您尚未发布堆栈跟踪?是否正在发送integer?请查看我编辑的答案。并使用它发送intentwall.putExtra(“maxNumberPressed”,conttext.getText().toString())中的数据代码>编辑文本中的文本是否为整数??例如?我猜是1,2,50,40等等,对吗?如果你发送的值正确,你需要做一些检查。如果该值不是整数,则会出现错误。在发送值之前,请检查其是否为空。如果为空,则发送一些默认值,如0