将开关更改为“if-else”声明

发布于 2024-12-19 11:39:22 字数 1199 浏览 1 评论 0原文

我最近对编程产生了兴趣,我想为手机或平板电脑创建 Android 应用程序。几周内我已经从对 java/xml 几乎一无所知。我对此非常认真。我将以一种或另一种方式找到这个问题的答案。事实上,我希望在有人回答这个问题之前先弄清楚这一点。我已经解决了很多问题,而没有求助于任何人,但我在这个问题上停留了太久。我想我应该尝试一下。

我正在使用一个较旧的教程来构建一个练习 Twitter 应用程序(这些教程似乎无处不在,这就是我选择它的原因)。我使用 Eclipse 作为编辑器。

以下是本教程中的代码示例。这与我的问题相关:

@Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);

显然自从 ADT 14 推出以来,您不能再使用 (R.layout.timeline) 短语,错误消息将其称为“switch 语句”。

现在,在我在另一个网站上找到的一篇文章中,遇到类似问题的人展示了 Eclipse 中使用 ctrl+1 进行“快速修复”的屏幕截图。在屏幕截图中,弹出的修复显示“将 switch 转换为‘if-else’语句”。在我的 Eclipse 版本中没有弹出此修复程序。我的快速修复选项是“迁移 Android 代码”、“在“布局”类型中创建字段“时间线””、“在“布局”类型中创建常量“时间线””或“在文件中重命名”。

如果我选择“迁移 Android 代码”,则会弹出一个窗口,通知我 ADT 14 更新,以及如何在库项目中不再允许使用 switch 语句。它表示通过按 ctrl+1 进行快速修复,然后选择“切换到“if-else”语句”,就像我找到的屏幕截图中那样,将 switch 语句转换为“if-else”语句。但同样,当我这样做时,该选项不会弹出。

我更愿意知道代码中需要更改哪些内容,而不是知道如何弹出正确的快速修复。如果不是太麻烦的话,解释一下为什么这些变化到底是有影响力的将会非常有帮助。我的 java 文件中现在有很多(很多)错误,但其中大多数都是由于不同形式的这个确切问题造成的。如果我只能看到一个之前/之后的例子,我可能可以从那里找出它,而无需解释。但经过几个小时的搜索,到目前为止我还无法通过谷歌搜索找到它。

提前非常感谢....

并且作为记录,我在任何java文件中都没有看到任何开关类,如果这对答案有影响的话...

I've just recently become interested in programming, and I want to create Android apps for phones or tablets. I've come a long way in a couple weeks from knowing almost nothing about java/xml. I'm very serious about this. I'm going to find the answer to this question one way or the other. In fact, I hope to have it figured out before anyone answers this. I've fixed many issues without resorting to asking anyone, but I've just been stuck on this issue too long. I figured I'd give this a shot.

I'm using an older tutorial to build a practice twitter app (the tutorials for these seem to be everywhere, which is why I chose it). I'm using Eclipse for an editor.

The following is an example of code from the tutorial. which relates to my question:

@Override
    public void onCreate (Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.timeline);

Apparently since the intro of ADT 14, you can no longer use the (R.layout.timeline) phrase, which the error message refers to as a "switch statement."

Now, in a post I found on another site, someone who had a similar issue shows a screenshot of the "quick fix" in Eclipse using ctrl+1. in the screenshot, the fix that pops up says "convert switch to 'if-else' statement." This fix does not pop up in my version of Eclipse. My quick fix options are "migrate Android code", "create field 'timeline' in type 'layout'", "create constant 'timeline' in type 'layout'" or "rename in file."

If I choose "migrate Android code", a window pops up informing me of the ADT 14 update, and how switch statements are no longer allowed in library projects. It says to convert the switch statement to an "if-else" statement by pressing ctrl+1 for the quick fix, then choosing "switch to 'if-else' statement" like it does in the screenshot I found. But again, when I do this, that option does not pop up.

I would much rather know what needs to be changed in the code than know how to make the right quick fix pop up. If it isn't too much trouble, an explanation of why exactly these changes are affective would be very helpful. I have many (MANY) errors in my java files right now, but most of them are due to this exact problem in different forms. If I could see just one before/after example I could probably figure it out from there without an explanation. But after hours of searching, I cannot find that so far by googling.

Thanks so much in advance....

And for the record, I don't see any switch classes in any of my java files, if that makes a difference for the answer...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

救星 2024-12-26 11:39:22

确保点击 switch 关键字本身,然后按 Ctrl + 1
这一开始也让我很困惑...

如果它仍然没有出现,你使用的是哪个版本的 Eclipse?

如果您使用的是 Mac,请选择关键字 switch 并单击 Shift + Command + 1
这将显示将 switch 更改为 if else 条件的提示。

Make sure you click on the switch keyword itself then press Ctrl + 1.
This confused me at first as well...

If it still doesn't show up, what version of Eclipse are you using?

If you are using a Mac select the keyword switch and click Shift + Command + 1.
That will show a prompt to change switch to if else conditions.

一江春梦 2024-12-26 11:39:22

我也遇到了同样的问题。对我来说,我正在切换 view.getId()。在切换之前,声明 int id = view.getId();。然后切换id。然后您可以按 ctrl+1 单击,然后会弹出“将开关转换为 If/Else”。

他们进行此更改是为了降低构建速度。现在,我的项目构建时间大约为 1/10。我很高兴升级了 ADT。

I've been having the same issues. For me, I was switching on view.getId(). Before the switch, declare int id = view.getId();. Then switch in id. Then you can ctrl+1 click and the "Convert Switch to If/Else" should pop up.

They made this change to decrease the build speed. My projects now build in roughly 1/10th of the time. I'm glad I upgraded the ADT.

久伴你 2024-12-26 11:39:22

出现此问题的原因是,自 ADT 14 以来,资源标识符不再是最终的。
您可以使用 if-else 语句更改 switch 语句的快速解决方案。

使用 Android Studio 4.1.3

步骤:1。只需右键单击语句的switch关键字即可。

步骤:2 单击第一个选项显示上下文操作

步骤:3 单击将switch 替换为if

完成

The issue happens because since ADT 14 resource identifiers are no longer final.
The quick solution you can change switch statements with if-else statements.

With Android Studio 4.1.3

Step:1. Just right click on the switch keyword of statement.

Step:2 Click on the first option Show Context Actions.

Step:3 Click replace switch with if.

Done

情话难免假 2024-12-26 11:39:22

在我看来,您似乎没有时间表资源。

分解R.layout.timeline

R指的是资源

layout指的是应用程序中布局的集合

时间轴也指您尝试应用setContentView()的特定资源。

如果我的预感是正确的,那么您就没有时间轴资源。
在 Eclipse 的布局文件夹中创建一个新的 XML 文件并将其命名为“timeline”
这应该可以解决问题。

To me, it looks like you don't have a timeline resource.

Breaking down R.layout.timeline

R refers to Resources

layout refers to the collection of layouts in your application

and timeline refers the specific resource that you are trying to apply setContentView() too.

If my hunch is correct, you don't have the timeline resource.
Create a new XML file in your layout folder in eclipse and name it 'timeline'
That should resolve the issue.

梦里的微风 2024-12-26 11:39:22

我希望你一切顺利。

我同意这听起来像是一些奇怪的 Eclipse 错误,但事实并非如此,也不是 Quick Fix 所说的那样。众所周知,Eclipse 有时会撒谎。清理你的项目(正如 @Stephen Dubya 所说),删除有问题的代码,保存文件,将其粘贴回来,然后再次保存......这些是有时让 Eclipse 表现出来的一些不明显的技巧。

一般来说,我认为您更好地熟悉一些 Java 基础知识,例如 switch;我认为这会让你学习 Android 变得更加容易。尽管工具和文档不断改进,但 Android 还很年轻,使用教程并不总是很容易学习,尤其是当您根本不习惯编写代码或使用 Eclipse 等复杂的 IDE 时。

但我离题了。仅适用于 Android库项目,不是常规项目,Android 不处理资源作为常数(final 变量),自 ADT 14 起。这意味着在库项目中,您不能在 中使用 R.layout.timeline 或类似的资源变量switch 语句。您只能切换整数或枚举值。

无论如何,始终使用 if-else 可能是您最好的选择。 switch 可能会导致代码中的逻辑错误,尤其是对于初学者而言,并且使用 switch 编写的任何内容都可以使用 if-else 重写块。就我个人而言,我已经完全停止在 Android 和其他 Java 代码中使用 switch 了。

I hope you got things working.

I agree that this sounds like some weird Eclipse error that is not what it seems, and not what Quick Fix says it is. Eclipse has been known to lie from time to time. Cleaning your project (as @Stephen Dubya said), cutting out the offending code, saving the file, pasting it back in, and saving again...these are some of the non-obvious tricks that sometimes get Eclipse to behave.

In general, I think it would be good for you to get more familiar with some of the Java basics like switch; I think it will make your learning of Android a lot easier. Although the tools and documentation keep improving, Android is still young and not always easy to learn using tutorials, especially when you aren't used to writing code at all or using complicated IDEs like Eclipse.

But I digress. It is only in Android library projects, not regular projects, that Android doesn't treat resources as constants (final variables), since ADT 14. That means means that in library projects, you can't use R.layout.timeline or similar resource variables in your switch statements. You can only switch on whole numbers or enum values.

Using if-else all the time may be your best bet, anyway. switch can, especially for beginners, lead to logic errors in your code, and anything you can write using switch can be rewritten using if-else blocks. Personally, I've stopped using switch in Android and other Java code altogether.

墨离汐 2024-12-26 11:39:22

您需要将诅咒放在单词开关中的 s 之前,然后按 cmd 1 。如果您的 switch 语句具有失败子句,则转换选项将不可用。

You need to place the curse right before the s in the word switch, and the press cmd 1 . If you switch statements have fall-through clauses, the option to convert won't be available.

感性 2024-12-26 11:39:22

不知怎的,我过度劳累了这个错误。我已从工作区中删除了我的项目。将其 .project 文件恢复为在 SVN 提交的旧文件。然后在 Eclipse 中卸载 ADT 并再次安装。然后导入我的项目。 ->库以旧方式添加,因此 R.java 中的所有字段现在都是最终的。希望这有帮助。

Somehow I overworked this error. I have deleted my project from workspace. Reverted it's .project files to old ones commited at SVN. Then at Eclipse uninstalling ADT and install it again. Then import my project. - > Libraries are added the old way and as result all fields at R.java are now final. Hope this helps.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文