2017-03-02 140 views
0

我有这样的代码:GoogleApiAvailability.getErrorDialog()无法找到字符串资源

GoogleApiAvailability googleAPI = GoogleApiAvailability.getInstance(); 
String msg = getString(R.string.common_google_play_services_update_text); 
Log.e(TAG, msg); 
Dialog errDlg = googleAPI.getErrorDialog(MyActivity.this, result, 1111, listener); 

在运行此,该字符串common_google_play_services_update_text被正确地写入LogCat,但getErrorDialog()抛出此异常:

java.lang.NoSuchFieldError: No static field common_google_play_services_update_text of type I in class Lcom/google/android/gms/R$string; or its superclasses (declaration of 'com.google.android.gms.R$string' appears in /data/app/com.mygame-1/base.apk)

我该如何解决这个问题?

回答

0

错误NoSuchFieldError表示该类没有指定名称的字段。如果应用程序试图访问或修改对象的指定字段,并且该对象不再具有该字段,则会引发此问题。通常,编译器会捕获此错误,并且只能在运行时发生,如果某个类的定义发生了不兼容的更改。

此外,也许你已经有了旧的代码引用了一个不存在于重新编译的类文件中的字段。你可以检查它here

The solution is to clean out all the class files and compile everything from fresh.

Update: If you still get the same error after recompiling everything, then you're probably compiling against one version of an external library and using another at runtime.

What you need to do now is first identify the class that is causing the problem (it looks like you have done this already) and then run your application with the -verbose:class command line option. It will dump a lot of class loading information on your standard out and you'll be able to find out where the problematic class is exactly loaded from.

希望这有助于!

+0

当代码运行时,字符串common_google_play_services_update_text被正确写入LogCat。所以它必须是getErrorDialog()的错误。 – activity