2

我正在尝试将对象从服务器应用程序发送到 android 客户端。所有的类都在同一个包中,并且在 Eclipse 的 src 文件夹中,包括导致 ClassNotFoundException: CardGame 的类。我是android新手,对java还很陌生,并且已经坚持了几天,所以我想知道是否有人能够帮助我找出问题所在......

这是导致问题的代码...

CardGame game = (CardGame) in.readObject();

这是堆栈跟踪...

04-21 19:21:35.608: W/System.err(482): java.lang.ClassNotFoundException: CardGame
04-21 19:21:35.618: W/System.err(482):  at java.lang.Class.classForName(Native Method)
04-21 19:21:35.618: W/System.err(482):  at java.lang.Class.forName(Class.java:235)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2590)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1846)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:826)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:2066)
04-21 19:21:35.618: W/System.err(482):  at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:929)
04-21 19:21:35.628: W/System.err(482):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2285)
04-21 19:21:35.628: W/System.err(482):  at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2240)
04-21 19:21:35.628: W/System.err(482):  at com.client.activity.play.init(play.java:107)
04-21 19:21:35.628: W/System.err(482):  at com.client.activity.play.onCreate(play.java:54)
04-21 19:21:35.628: W/System.err(482):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2627)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2679)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread.access$2300(ActivityThread.java:125)
04-21 19:21:35.628: W/System.err(482):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2033)
04-21 19:21:35.638: W/System.err(482):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-21 19:21:35.638: W/System.err(482):  at android.os.Looper.loop(Looper.java:123)
04-21 19:21:35.638: W/System.err(482):  at android.app.ActivityThread.main(ActivityThread.java:4627)
04-21 19:21:35.638: W/System.err(482):  at java.lang.reflect.Method.invokeNative(Native Method)
04-21 19:21:35.638: W/System.err(482):  at java.lang.reflect.Method.invoke(Method.java:521)
04-21 19:21:35.638: W/System.err(482):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-21 19:21:35.638: W/System.err(482):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-21 19:21:35.648: W/System.err(482):  at dalvik.system.NativeStart.main(Native Method)
04-21 19:21:35.648: W/System.err(482): Caused by: java.lang.NoClassDefFoundError: CardGame
04-21 19:21:35.648: W/System.err(482):  ... 24 more
04-21 19:21:35.658: W/System.err(482): Caused by: java.lang.ClassNotFoundException: CardGame in loader dalvik.system.PathClassLoader[/data/app/com.client.activity-1.apk]
04-21 19:21:35.658: W/System.err(482):  at dalvik.system.PathClassLoader.findClass(PathClassLoader.java:243)
04-21 19:21:35.668: W/System.err(482):  at java.lang.ClassLoader.loadClass(ClassLoader.java:573)
04-21 19:21:35.668: W/System.err(482):  at java.lang.ClassLoader.loadClass(ClassLoader.java:532)
04-21 19:21:35.668: W/System.err(482):  ... 24 more

谢谢!

4

3 回答 3

4

事实证明,通过 ObjectOutputStream 发送的对象的类在服务器和客户端设备上必须具有相同的包名称。这似乎解决了我的问题。感谢大家的帮助。

于 2012-04-22T16:46:03.733 回答
1

值得注意的是,不同 VM(例如 Android 的 Delvik 和 Sun JVM)之间的序列化可能不兼容。

我建议如果您进行服务器/Android 通信,那么您应该依赖 XML 或 JSON 之类的东西(我更喜欢后者,并且有一些 Android 类可以使它更容易)

见:http: //developer.android.com/reference/org/json/JSONObject.html

于 2012-04-22T15:39:47.620 回答
0

It's hard to say much more without being able to see your Eclipse setup, but that error indicates that your CardGame class is not ending up getting package into the APK that's getting built.

Is that class in the Android project or is it in the Server project? If it's in the server project, make sure that project is being referenced by the Android project and that your CardGame class is getting exported properly and is visible in that project.

If you look at the source code in Eclipse with the line:

CardGame game = (CardGame) in.readObject();

Is it finding CardGame OK? If not, then your classpath setup is bad. If yes, then the Android build is not pulling in all the necessary resources when building the APK.

于 2012-04-21T19:53:10.897 回答