无法在 Intent 中传递整数
Cannot pass an Integer in an Intent
我正在尝试通过意图将整数(从编辑文本)传递给另一个 activity。
当用户单击一个按钮时,edittext 中的文本将转换为字符串,然后转换为 int,然后 int 将通过 intent 发送到另一个 activity,但之后我必须使用 int。
此处 activity 发送意图:
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
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);
这里 activity 收到它:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
int wall = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
wall = getIntent().getIntExtra("maxPressed", -1);
randone = (int) (Math.random()*10+1);
contone = 0;
}
我正在使用它:
public void guessone (View view){
contone++;
textcontone.setText(getString(R.string.attempts) + "" + contone);
if (contone >= wall ){
resultaone.setText("You Failed" + " " + wall);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
当我使用该应用程序时,int 的值始终为 -1。我哪里错了。
在 activity 你收到它:
wall = getIntent().getIntExtra("maxPressed");
如果不将 xml 设置为 activity,则无法使用 findViewById
。这意味着只有在调用 setContentView
之后才需要使用 findViewById
方法。
此外,您还需要在单击按钮后读取 EditText 文本值,否则它将始终为 null/empty。
这样做
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext;
Button buttone;
String maxom;
int maxam = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
View.OnClickListener maxim = new View.OnClickListener() {
@Override
public void onClick (View view) {
maxom = conttext.getText().toString();
maxam = Integer.parseInt(maxom);
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
public int getIntExtra (String name, int defaultValue)
Added in API level 1 Retrieve extended data from the intent.
Parameters name The name of the desired item. defaultValue the value
to be returned if no value of the desired type is stored with the
given name. Returns the value of an item that previously added with
putExtra() or the default value if none was found. See Also
putExtra(String, int)
这意味着当您调用 getIntExtra(valueName, defaultValue);
时没有找到 int,因此选择了默认值。
在调用新的 activity 之前,您应该检查一下 maxam
值是多少。
问题 1
改为将其放入点击侦听器中:
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
您希望在单击按钮时而不是在打开 activity 时读取值,对吗?
问题2
以下需要在onCreate
中的setContentView
之后:
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
将声明保留在原处。只是声明:
EditText conttext;
Button buttone;
备注
在您的所有活动中遵循相同的模式。将视图声明为字段变量,在 setContentLayout
之后的 onCreate
中分配它们。在需要时获取值。
已解决:通过 getInt 和 String.valueOf
private static final String IMGID = "ImgID";
if (getIntent().getExtras().containsKey(IMGID)) {
//Picasso.with(this).load(getIntent().getExtras().getString(IMG)).into(mImg);
Picasso.with(this).load(getIntent().getExtras().getInt(String.valueOf(IMGID))).into(mImg);
}
我正在尝试通过意图将整数(从编辑文本)传递给另一个 activity。 当用户单击一个按钮时,edittext 中的文本将转换为字符串,然后转换为 int,然后 int 将通过 intent 发送到另一个 activity,但之后我必须使用 int。
此处 activity 发送意图:
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
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);
这里 activity 收到它:
public class GuessOne extends ActionBarActivity {
int randone;
int contone;
int wall = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_guess_one);
wall = getIntent().getIntExtra("maxPressed", -1);
randone = (int) (Math.random()*10+1);
contone = 0;
}
我正在使用它:
public void guessone (View view){
contone++;
textcontone.setText(getString(R.string.attempts) + "" + contone);
if (contone >= wall ){
resultaone.setText("You Failed" + " " + wall);
Toast.makeText(this, "You Failed", Toast.LENGTH_LONG).show();
}
当我使用该应用程序时,int 的值始终为 -1。我哪里错了。
在 activity 你收到它:
wall = getIntent().getIntExtra("maxPressed");
如果不将 xml 设置为 activity,则无法使用 findViewById
。这意味着只有在调用 setContentView
之后才需要使用 findViewById
方法。
此外,您还需要在单击按钮后读取 EditText 文本值,否则它将始终为 null/empty。
这样做
public class HomeActivityPro extends ActionBarActivity {
private InterstitialAd interstitial;
EditText conttext;
Button buttone;
String maxom;
int maxam = -1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_home);
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
View.OnClickListener maxim = new View.OnClickListener() {
@Override
public void onClick (View view) {
maxom = conttext.getText().toString();
maxam = Integer.parseInt(maxom);
Intent wall = new Intent(HomeActivityPro.this, GuessOne.class);
wall.putExtra("maxPressed", maxam);
startActivity(wall);
}
};
buttone.setOnClickListener(maxim);
public int getIntExtra (String name, int defaultValue)
Added in API level 1 Retrieve extended data from the intent.
Parameters name The name of the desired item. defaultValue the value to be returned if no value of the desired type is stored with the given name. Returns the value of an item that previously added with putExtra() or the default value if none was found. See Also putExtra(String, int)
这意味着当您调用 getIntExtra(valueName, defaultValue);
时没有找到 int,因此选择了默认值。
在调用新的 activity 之前,您应该检查一下 maxam
值是多少。
问题 1
改为将其放入点击侦听器中:
String maxom = conttext.getText().toString();
int maxam = Integer.parseInt(maxom);
您希望在单击按钮时而不是在打开 activity 时读取值,对吗?
问题2
以下需要在onCreate
中的setContentView
之后:
conttext = (EditText) findViewById ( R.id.texthome );
buttone = (Button) findViewById(R.id.buttone);
将声明保留在原处。只是声明:
EditText conttext;
Button buttone;
备注
在您的所有活动中遵循相同的模式。将视图声明为字段变量,在 setContentLayout
之后的 onCreate
中分配它们。在需要时获取值。
已解决:通过 getInt 和 String.valueOf
private static final String IMGID = "ImgID";
if (getIntent().getExtras().containsKey(IMGID)) {
//Picasso.with(this).load(getIntent().getExtras().getString(IMG)).into(mImg);
Picasso.with(this).load(getIntent().getExtras().getInt(String.valueOf(IMGID))).into(mImg);
}