我有一个班级'数据',其定义如下:
public class Data implements Serializable{
/**
*
*/
private static final long serialVersionUID = 112358L;
public int pagenumber;
public int chapternumber;
public int marks;
public Data(int pn, int c, int m) {
chapternumber=c;
pagenumber=pn;
marks=m;
} }
我在NETBEANS中为java创建了一个.brd文件(我自己的文件类型),如下所示:
fout=new FileOutputStream("Data.brd");
oos=new ObjectOutputStream(fout);
oos.writeObject(list);
其中list是DataList的数据列表。
我想从我的Android应用程序中读取此文件。 所以我在我的包中创建了完全相同的类,并将Data.brd保存在我的assets文件夹中。
ArrayList<Data> data = null; //object to be deserialized
InputStream is = null;
ObjectInputStream ois=null;
AssetManager assets = getAssets();
is = assets.open("Data.brd");
ois = new ObjectInputStream(is);
data = (ArrayList<Data>) ois.readObject();
但我将Data作为空白ArrayList。 除此之外,我得到以下例外:
我做错了什么? 我正在为Android应用程序使用Eclipse IDE。06-13 17:29:12.320:W / System.err(2180): java.lang.ClassNotFoundException:software.Data 06-13 17:29:12.320: W / System.err(2180):at java.lang.Class.classForName(Native Method) 06-13 17:29:12.324:W / System.err(2180):at java.lang.Class.forName(Class.java:217)06-13 17:29:12.324: W / System.err(2180):at java.io.ObjectInputStream.resolveClass(ObjectInputStream.java:2301) 06-13 17:29:12.324:W / System.err(2180):at java.io.ObjectInputStream.readNewClassDesc(ObjectInputStream.java:1660) 06-13 17:29:12.324:W / System.err(2180):at java.io.ObjectInputStream.readClassDesc(ObjectInputStream.java:683) 06-13 17:29:12.324:W / System.err(2180):at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1803) 06-13 17:29:12.328:W / System.err(2180):at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787) 06-13 17:29:12.328:W / System.err(2180):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003) 06-13 17:29:12.328:W / System.err(2180):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960) 06-13 17:29:12.328:W / System.err(2180):at java.util.ArrayList.readObject(ArrayList.java:657)06-13 17:29:12.328: W / System.err(2180):at java.lang.reflect.Method.invokeNative(Native 方法)06-13 17:29:12.328:W / System.err(2180):at java.lang.reflect.Method.invoke(Method.java:511)06-13 17:29:12.328: W / System.err(2180):at java.io.ObjectInputStream.readObjectForClass(ObjectInputStream.java:1354) 06-13 17:29:12.328:W / System.err(2180):at java.io.ObjectInputStream.readHierarchy(ObjectInputStream.java:1266) 06-13 17:29:12.332:W / System.err(2180):at java.io.ObjectInputStream.readNewObject(ObjectInputStream.java:1855) 06-13 17:29:12.332:W / System.err(2180):at java.io.ObjectInputStream.readNonPrimitiveContent(ObjectInputStream.java:787) 06-13 17:29:12.332:W / System.err(2180):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:2003) 06-13 17:29:12.332:W / System.err(2180):at java.io.ObjectInputStream.readObject(ObjectInputStream.java:1960) 06-13 17:29:12.332:W / System.err(2180):at com.mainpackage.MainActivity.onCreate(MainActivity.java:84)06-13 17:29:12.332:W / System.err(2180):at android.app.Activity.performCreate(Activity.java:5008)06-13 17:29:12.336:W / System.err(2180):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1079) 06-13 17:29:12.336:W / System.err(2180):at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2023) 06-13 17:29:12.336:W / System.err(2180):at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2084) 06-13 17:29:12.336:W / System.err(2180):at android.app.ActivityThread.access $ 600(ActivityThread.java:130)06-13 17:29:12.336:W / System.err(2180):at android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1195) 06-13 17:29:12.336:W / System.err(2180):at android.os.Handler.dispatchMessage(Handler.java:99)06-13 17:29:12.336:W / System.err(2180):at android.os.Looper.loop(Looper.java:137)06-13 17:29:12.336: W / System.err(2180):at android.app.ActivityThread.main(ActivityThread.java:4745)06-13 17:29:12.336:W / System.err(2180):at java.lang.reflect.Method.invokeNative(Native Method)06-13 17:29:12.340:W / System.err(2180):at java.lang.reflect.Method.invoke(Method.java:511)06-13 17:29:12.340: W / System.err(2180):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:786) 06-13 17:29:12.340:W / System.err(2180):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)06-13 17:29:12.340:W / System.err(2180):at dalvik.system.NativeStart.main(Native Method)06-13 17:29:12.340: W / System.err(2180):引起:java.lang.NoClassDefFoundError: 软件/数据06-13 17:29:12.344:W / System.err(2180):... 33更多 06-13 17:29:12.344:W / System.err(2180):引起: java.lang.ClassNotFoundException:software.Data 06-13 17:29:12.344: W / System.err(2180):at dalvik.system.BaseDexClassLoader.findClass(BaseDexClassLoader.java:61) 06-13 17:29:12.344:W / System.err(2180):at java.lang.ClassLoader.loadClass(ClassLoader.java:501)06-13 17:29:12.344:W / System.err(2180):at java.lang.ClassLoader.loadClass(ClassLoader.java:461)
答案 0 :(得分:0)
我知道你在另一个项目中创建了类Data。你必须在jar中打包该类并将其添加到你的android项目的buildpath中。
如果您只是在Android项目中重新创建Data类,那么它将与ClassLoader不同。