2014-01-13 6 views
5

私のにはandroidアプリケーションで私はライセンスデータを格納するためにファイルを使用します。そして、オブジェクトをシリアル化するを使用します。 デバイスオブジェクトを作成し、オブジェクトのファイル詳細を読み込みます。 デバイスクラスはSerializableを実装しています。Android NotSerializableExceptionはオブジェクトに対して発生します

public class MyDevice implements Serializable {} 

しかし、アプリケーションの開始時に、それはをデシリアライズとMYDEVICEオブジェクトに格納します。私のdeserializeObjectメソッドは以下の通りです。

public MyDevice deserializeObject() { 

    File SerialFile = new File(GeoTrackerPaths.FILE_PATH); 
    MyDevice AndDeviceIn = new MyDevice(); 

    if (SerialFile.exists()) { 
     try { 
      FileInputStream fileIn = new FileInputStream(GeoTrackerPaths.FILE_PATH); 
      ObjectInputStream objInput = new ObjectInputStream(fileIn); 
      AndDeviceIn = (MyDevice) objInput.readObject(); 
      objInput.close(); 
      fileIn.close(); 

     } catch (Exception e) { 

      Log.i("TAG", "Exception during deserialization:" + e.getMessage()); 
      e.printStackTrace(); 
      System.exit(0); 
     } 
    } 

    return AndDeviceIn; 
} 

私のシリアル化コード

public void serializeObject(Context context, String phoneModel, 
     String androidVersion, String executiveCode, String Key, 
     String modelID, String tempKey, int noLogin, String expireDate, String Status) { 

    try { 
     MyDevice AndDeviceOut = new MyDevice(context, phoneModel, 
       androidVersion, new Date(), executiveCode, Key, modelID, 
       tempKey, noLogin, expireDate, Status); 

     FileOutputStream fileOut = new FileOutputStream(
       GeoTrackerPaths.FILE_PATH); 
     ObjectOutputStream objOutput = new ObjectOutputStream(fileOut); 
     objOutput.writeObject(AndDeviceOut); 
     objOutput.flush(); 
     objOutput.close(); 
     fileOut.close(); 

    } catch (Exception e) { 
     Log.i("TAG", "Exception during serialization:" + e.getMessage()); 
     e.printStackTrace(); 
     System.exit(0); 
    } 
} 

そして、私は以下のようにそれを呼んでいます。

DeviceActivator activate=new DeviceActivator(); 
activate.serializeObject(Activation.this, phoneModel, androidVersion, txtExe, exeKey, modeilID, tempKey, noLogin, expireDate, Activation_Status); 

私が発生した例外以下のアプリを実行していますよ。

java.io.WriteAbortedException: Read an exception; 
java.io.NotSerializableException: com.geotracker.entity.MyDevice 

どうすればこの問題を解決できますか?

+0

例外はありませんが、新しいMyDeviceオブジェクトを作成してから、ファイルから読み込んだオブジェクトに同じ参照を割り当てる理由は何ですか? –

+0

あなたは "MyDevice AndDeviceIn = new MyDevice();"を意味します。ではない。オブジェクトを格納するためのMyDeviceインスタンスを作成してはいけません.......... –

+0

シリアライズ解除コードの例は間違っていますが、シリアライズ処理を行うコードに問題がある可能性があります。その部分も投稿できますか? – Durandal

答えて

4

Android Contextオブジェクトはシリアライズ可能ではありません。これを解決するには、Contextオブジェクトを一時的なものとして宣言します。これについては、JDK仕様のLinkを参照してください。

private transient Context context;

、あなたが行くように良いことがあります。基本的には一時的なものとしてフィールドをマークすることは、そうMyDeviceにこのようなあなたのフィールドを宣言シリアライズ

に参加しないことを意味します!

関連する問題