Android 资源 ID 突然不是最终的,字段无法解析

发布于 2024-12-11 23:35:27 字数 1560 浏览 0 评论 0原文

我正在开发一个新旧项目...我正在为不同的国家/地区市场制作它,当我从旧项目复制代码时,它显示一些错误,例如迁移 ANDROID 代码(从 ADT 14 开始,资源字段不能用作 switch case。)这种情况发生在 strings.xml 文件中的资源和布局中。 但它没有给我一些可能的修复...我该如何修复它???这是代码:

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Here is mistake
            setContentView(R.layout.main);

          //Kreira AlertDialog sa dva dugmeta koji ce se pojaviti pri aktiviranju aplikacije
            ad = new AlertDialog.Builder(this).create();
//And HERE
            ad.setTitle(getString(R.string.vasa_trenutna_lokacija));
            //And HERE
ad.setMessage(getString(R.string.da_bi_ste_koristili_aplikaciju));
            //And HERE
            ad.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.dozvoli), new DialogInterface.OnClickListener() {

            //Klikom na dugme Dozvoli otvara se novi prozor
            @Override
            public void onClick(DialogInterface ad, int which) {
                Intent i = new Intent(NiskiMerakActivity.this, TrenutnaLokacija.class);             
                startActivity(i);
                finish();
                }
            });

            ad.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.ne_dozvoli),new DialogInterface.OnClickListener() {

            //Klikom na dugme Ne dozvoli aplikacija se zatvara
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });

I'm working on a new-old project... I'm making it for a different country market, and when i copyed my code from an old project it shows some mistake like MIGRATE ANDROID CODE (As of ADT 14, resource fields cannot be used as switch cases.) This happens for a resourses that are in strings.xml file and for layout.
But it doesn't give me some posible fix... how can i fix it???? Here is code:

 @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            //Here is mistake
            setContentView(R.layout.main);

          //Kreira AlertDialog sa dva dugmeta koji ce se pojaviti pri aktiviranju aplikacije
            ad = new AlertDialog.Builder(this).create();
//And HERE
            ad.setTitle(getString(R.string.vasa_trenutna_lokacija));
            //And HERE
ad.setMessage(getString(R.string.da_bi_ste_koristili_aplikaciju));
            //And HERE
            ad.setButton(DialogInterface.BUTTON_POSITIVE, getString(R.string.dozvoli), new DialogInterface.OnClickListener() {

            //Klikom na dugme Dozvoli otvara se novi prozor
            @Override
            public void onClick(DialogInterface ad, int which) {
                Intent i = new Intent(NiskiMerakActivity.this, TrenutnaLokacija.class);             
                startActivity(i);
                finish();
                }
            });

            ad.setButton(DialogInterface.BUTTON_NEGATIVE, getString(R.string.ne_dozvoli),new DialogInterface.OnClickListener() {

            //Klikom na dugme Ne dozvoli aplikacija se zatvara
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    finish();

                }
            });

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

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

发布评论

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

评论(10

路还长,别太狂 2024-12-18 23:35:27

引用 http://tools.android.com/tips/non-constant-fields

解决方案很简单:将 switch 语句转换为 if-else 语句。

由于上面的代码片段似乎没有 switch() 语句,但您的问题引用了 switch() 语句,我假设您的代码错误。

Quoting http://tools.android.com/tips/non-constant-fields

The solution for this is simple: Convert the switch statement into an if-else statement.

Since your code snippet above does not appear to have a switch() statement, yet your question refers to a switch() statement, I assume you have the wrong code.

临走之时 2024-12-18 23:35:27

如果您的 ids 开关盒位于库内,则遗憾的是新的 ADT 版本不支持它。

但是,您可以轻松地将其转换为 if-else ,如 Google 网站上所示:

http://tools.android.com/tips/non-constant-fields

if your switch-case of the ids is inside a library, sadly the new ADT versions don't support it.

However, you can convert it easily to if-else , as shown on Google's website:

http://tools.android.com/tips/non-constant-fields
左秋 2024-12-18 23:35:27

从 ADT 14 开始,资源字段不能用作 switch case。

这也发生在我身上,即使我没有在 switch 语句中使用 R 变量,我也会收到此错误。不要被错误消息误导。您基本上有一些与 R.java 文件相关的错误。确保您不导入 R.java,因为您应该拥有自己的。删除 R.java 文件,构建项目,然后查看是否生成 R.java 文件。如果没有,您可能会遇到错误,导致 R.java 文件无法更新。尝试项目>清理以发现任何错误。消除错误并重建项目,以便生成 R.java 文件并保持最新。

As of ADT 14, resource fields cannot be used as switch cases.

This happened to me too, I got this error even though I wasn't using the R variable in a switch statement. Don't be mislead by the error message. You basically have some error that relates to the R.java file. Make sure you don't import R.java since you should have your own. Delete your R.java file, build project, and see if your R.java file gets generated. If not, you could have an error that is preventing your R.java file from being updated. Try Project > Clean to find any errors. Get rid of the errors and rebuild your project so that your R.java file gets generated and is up-to-date.

迟月 2024-12-18 23:35:27

也许您只需要Clean您的项目,即Project ->从主菜单清理。
上面的代码不应该有错误。问题仅在于 switch 语句。
请尝试清理并在此评论结果是什么。

Probably you just need to Clean your project, i.e. Project -> Clean from the main menu.
There should not be errors in the code above. The problem is only with switch statements.
Please try to clean and comment here what is the result.

乖乖 2024-12-18 23:35:27

我通过将导入从 android.r; 更改为 android.R.*; 解决了这个问题,

它清除了我的所有错误,我能够再次编译我的应用程序。

I fixed this by changing my import from android.r; to android.R.*;

It cleared all of my errors and i was able to compile my app again.

谁许谁一生繁华 2024-12-18 23:35:27

今天我遇到了同样的错误,我的 eclipse 项目添加了 1 个项目库,我打算将所有代码更改为 if 语句,但我意识到我的项目在项目 -> 属性 -> 中检查了库。 android,所以我只是将其关闭,这就是问题的解决方案,这也是 eclipse 本身的建议,仔细阅读 android 迁移代码 adt 14 项目库常量警告。

Today i was facing the same error, my eclipse project have 1 project library added, and i was about to change all the code to if statements, but i realize my project had the library check on, in project ->properties-> android, so i just turn it down and that was the solution to the problem, it also was a suggestion of eclipse itself, reading carefully android migration code adt 14 constants warning for project libraries.

静若繁花 2024-12-18 23:35:27

只需在eclipse项目中选择->干净了,问题就解决了。我只是通过在我的应用程序中创建一个新包并将其删除来得到此错误。

Just select in eclipse project -> clean, and the problem was solved. I got this error just by creating a new package in my app and removing it.

涫野音 2024-12-18 23:35:27

以下是帮助我解决此问题的步骤。

  1. 删除 R.java(Eclipse 将立即重新创建它。如果没有,则代码或布局有问题)
  2. 清理项目(Eclipse 菜单“项目”和“清理”
  3. 重新编译项目

这些步骤解决了我的工作问题。

Here are the steps which helps me to resolve this problem.

  1. Delete R.java (Eclipse will recreate it immediately. If not there is a problem with the code or layout)
  2. Clean the project (Eclipse menu 'Project' & 'Clean'
  3. Recompile the project

These steps resolve the problem with my work.

笑红尘 2024-12-18 23:35:27

我发现在更改 R 的导入语句以包含您的包名称后它可以工作。

例如:

导入android.R;

现在是

import com.name.package.R;

I found it works after changing the import statement for R to include your package name.

For instance:

import android.R;

is now

import com.name.package.R;

你的他你的她 2024-12-18 23:35:27

我尝试了上面提到的所有关于删除生成的 R.java 文件、清理、重建等的方法,但该方法对我不起作用。

有效的方法是将案例替换为 else-if 链,在我看来,这是一个混乱的解决方案,但我无法让它以任何其他方式工作。

我正在使用 Eclipse 4.2.1 和 Android SDK 4.2.2

I tried all the approaches mentioned above regarding deleting the generated R.java file, clean, rebuild, etc but that approach did not work for me.

What did work was replacing the case with an else-if chain which was a messy fix in my opinion but I couldn't get it to work any other way.

I'm using Eclipse 4.2.1 and Android SDK 4.2.2

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