2017-03-02 138 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