Android 为什么这个函数传递的是字符串而不是int?
我想通过intent将R.drawable id传递给另一个活动。我使用一个开关来找到正确的可绘制图形,然后使用putExtra来传递它。但是当我调用getIntExtra时,它说我传递了一个字符串。这段代码有问题吗?还是在其他地方Android 为什么这个函数传递的是字符串而不是int?,android,android-intent,Android,Android Intent,我想通过intent将R.drawable id传递给另一个活动。我使用一个开关来找到正确的可绘制图形,然后使用putExtra来传递它。但是当我调用getIntExtra时,它说我传递了一个字符串。这段代码有问题吗?还是在其他地方 int i = getExerciseId(((TextView)getView().findViewById(R.id.fragTextView)).getText().toString()); Intent intent = new In
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");
用于获取int值
//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方法。称之为
getIntExtra("image", 0); // or whichever default value you want to use.
请张贴错误消息日志输出和代码,您正在提取额外的。哦,谢谢。我想我早些时候看过指南,但它是为getStringExtra准备的。
getIntExtra("image", 0); // or whichever default value you want to use.