Android 资源 ID 突然不是最终的,字段无法解析
我正在开发一个新旧项目...我正在为不同的国家/地区市场制作它,当我从旧项目复制代码时,它显示一些错误,例如迁移 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(10)
引用 http://tools.android.com/tips/non-constant-fields
由于上面的代码片段似乎没有
switch()
语句,但您的问题引用了switch()
语句,我假设您的代码错误。Quoting http://tools.android.com/tips/non-constant-fields
Since your code snippet above does not appear to have a
switch()
statement, yet your question refers to aswitch()
statement, I assume you have the wrong code.如果您的 ids 开关盒位于库内,则遗憾的是新的 ADT 版本不支持它。
但是,您可以轻松地将其转换为 if-else ,如 Google 网站上所示:
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:
从 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.
也许您只需要
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.
我通过将导入从
android.r;
更改为android.R.*;
解决了这个问题,它清除了我的所有错误,我能够再次编译我的应用程序。
I fixed this by changing my import from
android.r;
toandroid.R.*;
It cleared all of my errors and i was able to compile my app again.
今天我遇到了同样的错误,我的 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.
只需在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.
以下是帮助我解决此问题的步骤。
这些步骤解决了我的工作问题。
Here are the steps which helps me to resolve this problem.
These steps resolve the problem with my work.
我发现在更改 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;
我尝试了上面提到的所有关于删除生成的 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