2014-03-31 55 views
2

我想序列化android.net.Uri類型的對象的狀態。Serialize android.net.Uri對象

下面是我的writeObject模型類和readObject methoods

public class ReminderObject implements Serializable { 
private boolean isReminderOn; 
private int fromHours, toHours; 
private int interval; 
private ArrayList<CharSequence> daysToRepeat; 
private Uri toneToPlay; 
private AdvanceSettingsObject adv; 

public ReminderObject(boolean isReminderOn, int fromHours, int toHours, 
     int interval, ArrayList<CharSequence> daysToRepeat, Uri toneToPlay, 
     AdvanceSettingsObject adv) { 
    super(); 
    this.isReminderOn = isReminderOn; 
    this.fromHours = fromHours; 
    this.toHours = toHours; 
    this.interval = interval; 
    this.daysToRepeat = daysToRepeat; 
    this.toneToPlay = toneToPlay; 
    this.adv = adv; 
} 
/* 
getters and setters 
*/ 
    public void writeObject(ObjectOutputStream op){ 
    try { 
     op.defaultWriteObject(); 
     op.writeChars(toneToPlay.toString()); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 
} 
public void readObject(ObjectInputStream in){ 
    try{ 
     in.defaultReadObject(); 
     toneToPlay = Uri.parse(in.readUTF()); 
    }catch(Exception e){ 

    } 
} 

} 

代碼片段MainActivity:

try { 
     ObjectOutputStream os = new ObjectOutputStream(new  FileOutputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/ReminderData.txt") ); 
     os.writeObject(reminder); // Getting above mentioned Exception here 
     Log.i("TAG","reminder serialized"); 
     ObjectInputStream is = new ObjectInputStream(new FileInputStream(Environment.getExternalStorageDirectory().getAbsolutePath()+"/ReminderData.txt")); 
     ReminderObject reminderRead = (ReminderObject) is.readObject(); 
     if(reminderRead!=null) 
      Log.i("TAG", "Deserialized Reminder object is : "+reminderRead.toString()); 
     else{ 
      Log.i("TAG", "Null received"); 
     } 
    } catch(ClassNotFoundException cnf){ 
     cnf.printStackTrace(); 
    } catch (FileNotFoundException e) {  
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } catch (IOException e) { 
     // TODO Auto-generated catch block 
     e.printStackTrace(); 
    } 

異常面臨:

03-31 23:47:59.246: W/System.err(12681): java.io.NotSerializableException: android.net.Uri$HierarchicalUri 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1364) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeFieldValues(ObjectOutputStream.java:979) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.defaultWriteObject(ObjectOutputStream.java:368) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeHierarchy(ObjectOutputStream.java:1074) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeNewObject(ObjectOutputStream.java:1404) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObjectInternal(ObjectOutputStream.java:1671) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1517) 03-31 23:47:59.246: W/System.err(12681): at java.io.ObjectOutputStream.writeObject(ObjectOutputStream.java:1481) 03-31 23:47:59.246: W/System.err(12681): at com.navkar.navkarreminder.SetReminderActivity.scheduleReminder(SetReminderActivity.java:595)

請求幫助。

回答

6

正如我認爲問題是你保存問題的數據成員。

private Uri toneToPlay是一個非序列化數據類型,你不能序列化它。

+2

是,開放的我們是不是序列化的,所以我把它改成字符串。 – shahharshil46

+0

donot忘記標記答案solve.it幫助其他人找到答案。 – User10001

+0

'Uri' **是可序列化的** **檢查了這一點http://stackoverflow.com/questions/22533432/create-object-from-gson-string-doesnt-work – NecipAllef

2

URI private Uri toneToPlay不可序列化。因此,有一種替代方法,您可以將數據類型URI更改爲字符串,並將URI轉換爲字符串和字符串,然後在反之亦然時使用。

的URI字符串

Uri uri; 
String stringUri; 
stringUri = uri.toString(); 

字符串烏里

Uri uri; 
String stringUri; 
uri = Uri.parse(stringUri); 
相關問題