代码未执行[ListPreference]
当选择 ListPreference 中的选项时,我有这段代码进行一些处理:
choice = prefs.getString("listPref_kernel", "0");
if (choice == "0") {
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/jetpack/install");
dir.mkdirs();
File file = new File(dir, "talondev");
FileOutputStream f = new FileOutputStream(file);
}
catch (Exception e) {
Log.d("Excep_ERROR", e.getMessage());
}
}
就在这条指令之后: if (choice == "0")
没有执行任何操作,我放了一个吐司,但之后没有显示该指令但在此之前是的。 XML 文件:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="kernel">
<item>TalonDev</item>
<item>Semaphore</item>
<item>SpeedMod</item>
<item>Galaxian</item>
</string-array>
<string-array name="kernel_return">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
有什么想法吗?非常感谢。
I have this code to make some treatement when an option from ListPreference is selected:
choice = prefs.getString("listPref_kernel", "0");
if (choice == "0") {
try {
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/jetpack/install");
dir.mkdirs();
File file = new File(dir, "talondev");
FileOutputStream f = new FileOutputStream(file);
}
catch (Exception e) {
Log.d("Excep_ERROR", e.getMessage());
}
}
Just after this instruction: if (choice == "0")
nothing is executed, i put a toast and it's not showen after that instruction but before it yes.
XML FILE:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<string-array name="kernel">
<item>TalonDev</item>
<item>Semaphore</item>
<item>SpeedMod</item>
<item>Galaxian</item>
</string-array>
<string-array name="kernel_return">
<item>0</item>
<item>1</item>
<item>2</item>
<item>3</item>
</string-array>
</resources>
Any idea please? Thank you very much.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
如果它确实是 String 实例,则尝试
if(Integer.valueOf(choice) == 0)
If it is truely String instance then try to
if(Integer.valueOf(choice) == 0)
要么转换为数字,就像 Nikola 指出的那样,要么执行
if("0".equals(choice)) {..}
。无法使用 == 测试字符串变量是否相等。Either cast to a number, like Nikola pointed out, or do
if("0".equals(choice)) {..}
. String variables cannot be tested for equality with == .如果 choice 是整数,请尝试
if (choice == 0)
If choice is an integer try
if (choice == 0)