在学习JSON例子时遇到此错误,错误内容:
08-06 03:57:43.515: E/AndroidRuntime(513): Uncaught handler: thread main exiting due to uncaught exception
08-06 03:57:43.523: E/AndroidRuntime(513): java.lang.NoClassDefFoundError: com.google.gson.stream.JsonReader
08-06 03:57:43.523: E/AndroidRuntime(513): at mars.json01.JsonUtils.parseJson(JsonUtils.java:18)
08-06 03:57:43.523: E/AndroidRuntime(513): at mars.json01.MainActivity$ButtonListener.onClick(MainActivity.java:28)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.View.performClick(View.java:2364)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.View.onTouchEvent(View.java:4179)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.widget.TextView.onTouchEvent(TextView.java:6541)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.View.dispatchTouchEvent(View.java:3709)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
08-06 03:57:43.523: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
08-06 03:57:43.523: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
08-06 03:57:43.523: E/AndroidRuntime(513): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.os.Handler.dispatchMessage(Handler.java:99)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.os.Looper.loop(Looper.java:123)
08-06 03:57:43.523: E/AndroidRuntime(513): at android.app.ActivityThread.main(ActivityThread.java:4363)
08-06 03:57:43.523: E/AndroidRuntime(513): at java.lang.reflect.Method.invokeNative(Native Method)
08-06 03:57:43.523: E/AndroidRuntime(513): at java.lang.reflect.Method.invoke(Method.java:521)
08-06 03:57:43.523: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
08-06 03:57:43.523: E/AndroidRuntime(513): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
08-06 03:57:43.523: E/AndroidRuntime(513): at dalvik.system.NativeStart.main(Native Method)
08-06 03:57:43.574: I/dalvikvm(513): threadid=7: reacting to signal 3
08-06 03:57:43.574: E/dalvikvm(513): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
后来经过分析,我觉得是eclipse下自带有com.google.gson_××××.jar与项目中引用的gson.jar版本不一致,而程序中有没有明确指出用哪个,所以导致了该错误的发生
解决方式:右键点该项目,选Build Path-->Configure Build Path
选上gson,然后ok,然后关闭eclipse,重新打开后即可。
在解决过程中参考了:http://www.jili360.com/home-space-uid-1618-do-blog-id-1257.html
This fixed it for me:
- Remove all Android Library projects and external jars from the build path.
- Create a folder named 'libs' in your project.
- Place all external .jars in that folder, the ADT should now place them under 'Android Dependencies'.
- Re-import all your previous Android Library projects the normal way.
The issue is that external jars are not placed in the 'libs' folder by default when using Properties - Java Build Path- Add External Jar, instead they are placed under 'Referenced Libraries' instead of 'Android Dependencies' and this causes the NoClassDefFoundError.
Please, check Dealing with dependencies in Android projects for more details.