为什么这个 putExtra 传递的是 String 而不是 int?
Why is this putExtra passing a String instead of an int?
我想通过 intent 将一个 R.drawable id 传递给另一个 activity。我正在使用一个开关来找到正确的可绘制对象,然后使用 putExtra() 来传递它。但是当我调用 getIntExtra 时,它说我传递了一个字符串。这段代码有问题还是在其他地方?
int i = getExerciseId(((TextView)getView().findViewById(R.id.fragTextView)).getText().toString());
Intent intent = new Intent(v.getContext(), LevelActivity.class);
intent.putExtra("exerciseId", ((TextView)getView().findViewById(R.id.fragTextView)).getText().toString());
intent.putExtra("image", i);
startActivity(intent);
}
public int getExerciseId(String s) {
int i = -1;
switch (s) {
case "Weather":
i = R.drawable.weather;
break;
case "Weekend":
i = R.drawable.weekend;
break;
case "Hobbies":
i = R.drawable.hobbies;
break;
case "Music":
i = R.drawable.music;
break;
case "Family":
i = R.drawable.family;
}
return i;
编辑:没有日志错误,我在代码中收到一条红线消息:
getIntExtra(String, int) in Intent cannot be applied to (String)
在这行代码中:
intent.getIntExtra("image");
获取整数值
//wirte name=imge and defaultValue =-1
//and have a check if value for image is -1 then no value was received
intent.getIntExtra(name, defaultValue);
getIntExtra(String name, int defaultValue)
您必须为方法调用提供默认值。如果找不到该键,则返回默认值。
不是告诉你得到的是字符串,而是你调用的getIntExtra
方法参数不足。用
调用它
getIntExtra("image", 0); // or whichever default value you want to use.
我想通过 intent 将一个 R.drawable id 传递给另一个 activity。我正在使用一个开关来找到正确的可绘制对象,然后使用 putExtra() 来传递它。但是当我调用 getIntExtra 时,它说我传递了一个字符串。这段代码有问题还是在其他地方?
int i = getExerciseId(((TextView)getView().findViewById(R.id.fragTextView)).getText().toString());
Intent intent = new Intent(v.getContext(), LevelActivity.class);
intent.putExtra("exerciseId", ((TextView)getView().findViewById(R.id.fragTextView)).getText().toString());
intent.putExtra("image", i);
startActivity(intent);
}
public int getExerciseId(String s) {
int i = -1;
switch (s) {
case "Weather":
i = R.drawable.weather;
break;
case "Weekend":
i = R.drawable.weekend;
break;
case "Hobbies":
i = R.drawable.hobbies;
break;
case "Music":
i = R.drawable.music;
break;
case "Family":
i = R.drawable.family;
}
return i;
编辑:没有日志错误,我在代码中收到一条红线消息:
getIntExtra(String, int) in Intent cannot be applied to (String)
在这行代码中:
intent.getIntExtra("image");
获取整数值
//wirte name=imge and defaultValue =-1
//and have a check if value for image is -1 then no value was received
intent.getIntExtra(name, defaultValue);
getIntExtra(String name, int defaultValue)
您必须为方法调用提供默认值。如果找不到该键,则返回默认值。
不是告诉你得到的是字符串,而是你调用的getIntExtra
方法参数不足。用
getIntExtra("image", 0); // or whichever default value you want to use.