简体   繁体   中英

ClassNotFoundException when reading object from assets(android)

I have a class 'Data', which is defined like this :

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;
        } }

I have made a .brd file (my own file type) in NETBEANS for java in windows like this :

        fout=new FileOutputStream("Data.brd");
        oos=new ObjectOutputStream(fout);
        oos.writeObject(list);

Where list is an ArrayList of Data .

I want to read this file from my android app. so I made the exact same class inside my package and saved the Data.brd in my assets folder.

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();

But I get Data as a blank ArrayList. Aside of that, I get the following Exceptions:

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.ObjectInputS tream.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 Method) 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.jav a: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): a t 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): Caused by: java.lang.NoClassDefFoundError: software/Data 06-13 17:29:12.344: W/System.err(2180): ... 33 more 06-13 17:29:12.344: W/System.err(2180): Caused by: 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)

what am I doing wrong ? I am using Eclipse IDE for the android app.

I get that you created the class Data in a different project. You'll have to pack that class in a jar and add it to the buildpath of your android project.

If you simply recreate the Data class in your android project it will not be the same class to the ClassLoader.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM