简体   繁体   中英

ClassNotFoundException happens during calling ObjectInputStream.readobject in Android

I want to read object from Android internal storage.

The following is my code. I write a static function for reading object from file in the same class. No idea why this exception happen . Really appreciate if you could give some suggestions.

thanks.

package com.crescent.programmercalculator;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import android.util.Log;
import android.content.Context;

public class CalculateConfigurations implements Serializable{

    static String configLocation="configFile";

    public short radix;

    CalculateConfigurations(){
        radix=16; 
    }

    public static CalculateConfigurations loadObjectFromFile(Context context){
        try {
            FileInputStream fis = context.openFileInput(configLocation);
            ObjectInputStream is = new ObjectInputStream(fis);
            CalculateConfigurations config = (CalculateConfigurations) is.readObject();
            is.close();
            fis.close();
            return config;
        } catch (FileNotFoundException e) {
            // first use case
            Log.v("CalculateConfigurations", "first init for configuration file");

            return new CalculateConfigurations();
        } catch (IOException e) {
            e.printStackTrace();
            Log.e("CalculateConfigurations", "Fatal error, configuration file may be broken");

            return new CalculateConfigurations();
        } 
        catch (ClassNotFoundException e) {
            e.printStackTrace();
            Log.e("CalculateConfigurations", "Fatal error, unknown");

            return new CalculateConfigurations();
        }
    }
}

ClassNotFoundException

this is in most cases some mixup in your xml-files. If you post both stacktrace and the proper xml-file, it is easy to fix it!

Read on readObject

Exceptions are thrown for problems with the InputStream and for classes that should not be deserialized. All exceptions are fatal to the InputStream and leave it in an indeterminate state; it is up to the caller to ignore or recover the stream state.

Since you are trying to read an android internal library, it cannot be casted to your custom Class. Hence the ClassNotFoundException.

Hope this helps.

I encounter the same problem today. I don't know how the serialization/deserialization is done but I notice that it is very unstable process.

If you serialize some objects and put the memory, you cannot change the data type/class name easily.. If it throws class not found exception like this, remove the app from device and install again. I hope, it helps some developers :)

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