2014-09-29 58 views
0

我有2個類。在第一課中,我從List中寫入數據,在第二課中,我想讀取這些數據並放入列表視圖。它正確地保存數據,但是當我設法得到它,logcat的給出一個錯誤:ObjectInputStream不能正確轉換對象

java.lang.ClassCastException: pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline$podzialPlanu cannot be cast to pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$podzialPlanu 
      at pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$AdapterDlaPlanu.getView(AdapterPlanuProbOffline.java:115) 

我不明白爲什麼,因爲我總是得到相同的數據類型。

一流的寫入方法:

public void SaveAsFile(List<podzialPlanu> lista){ 
// lista - List<podzialPlanu> podzialPlanuList2 = new ArrayList<podzialPlanu>(); 

     try { 
      FileOutputStream saveFile = new FileOutputStream(Environment.getExternalStorageDirectory().getPath() + "/" + "planProby.sav"); 
      ObjectOutputStream save = new ObjectOutputStream(saveFile); 

      save.writeObject(lista); 

      save.close(); 


     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } 
    } 

和主類,當我嘗試從這個文件中獲取數據,並把它變成我的ListView

public class AdapterPlanuProbOffline extends Activity implements Serializable { 

    List<String> DzienDataList; 
    List<String> GodzinaList; 
    List<String> RodzajList; 
    List<String> ProgramList; 
    List<String> UwagiList; 
    List<String> listaDni; 
    List<String> listaMiesiecy; 
    File plik; 


    public class podzialPlanu implements Serializable{ 
     String DzienData; 
     String Godzina; 
     String Rodzaj; 
     String Program; 
     String Uwagi; 
    } 

    AdapterDlaPlanu nowyAdapter; 

    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.listviewy); 



     nowyAdapter = new AdapterDlaPlanu(); 

     ListView listView1 = (ListView) findViewById(R.id.listView1); 
     listView1.setAdapter(nowyAdapter); 

    } 



    public class AdapterDlaPlanu extends BaseAdapter implements Serializable{ 

     List<podzialPlanu> podzialPlanuList = getData(); 

     @Override 
     public int getCount() { 
      return podzialPlanuList.size(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return podzialPlanuList.get(position); 
     } 

     @Override 
     public long getItemId(int position) { 
      return position; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 

      if(convertView == null){ 
       LayoutInflater inflater = (LayoutInflater) AdapterPlanuProbOffline.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
       convertView = inflater.inflate(R.layout.planproby,parent,false); 
      } 



      TextView DzienDataTextView = (TextView) convertView.findViewById(R.id.dzienDataTextView); 
      TextView GodzinaTextView = (TextView) convertView.findViewById(R.id.godzinaTextView); 
      TextView RodzajZajecTextView = (TextView) convertView.findViewById(R.id.rodzajzajecTextView); 
      TextView ProgramTextView = (TextView) convertView.findViewById(R.id.programTextView); 
      TextView UwagiTextView = (TextView) convertView.findViewById(R.id.uwagiTextView); 




/// here is a problem with getData() method 
    DzienDataTextView.setText(getData().get(position).DzienData); 
    GodzinaTextView.setText(getData().get(position).Godzina); 
    RodzajZajecTextView.setText(getData().get(position).Rodzaj); 
    ProgramTextView.setText(getData().get(position).Program); 
    UwagiTextView.setText(getData().get(position).Uwagi); 



      return convertView; 
     } 

     public podzialPlanu getPodzialPlanu(int position){ 
      return podzialPlanuList.get(position); 
     } 
    } 




    public List<podzialPlanu> getData() { 

     List<podzialPlanu> podzialPlanuList2 = new ArrayList<podzialPlanu>(); 
     List< List<String> > listaGlowna = new ArrayList<List<String>>(); 


     for(int i = 0; i < 5; i++) { 
      listaGlowna.add(new ArrayList<String>()); 
     } 

     DzienDataList = new ArrayList<String>(); 
     GodzinaList = new ArrayList<String>(); 
     RodzajList = new ArrayList<String>(); 
     ProgramList = new ArrayList<String>(); 
     UwagiList = new ArrayList<String>(); 
     listaDni = new ArrayList<String>(); 
     listaMiesiecy = new ArrayList<String>(); 

     try { 

      FileInputStream file = new FileInputStream(Environment.getExternalStorageDirectory().getPath() + "/" + "planProby.sav"); 
      ObjectInputStream restore = new ObjectInputStream(file); 

      podzialPlanuList2 = (List<podzialPlanu>) restore.readObject(); 

      restore.close(); 

     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (StreamCorruptedException e) { 
      e.printStackTrace(); 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } catch (ClassNotFoundException e) { 
      e.printStackTrace(); 
     } 
     return podzialPlanuList2; 
    } 

回答

1

好了,讓我們看看例外信息:

java.lang.ClassCastException: 
      pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline$podzialPlanu 
cannot be cast to 
      pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline$podzialPlanu 

您是否看到區別?請注意,有兩個不同的podzialPlanu類。

現在,如果你的問題的SaveAsFile方法是創建序列化的一個,那麼它遵循的List<podzialPlanu>參數類型實際上是指一個:

pl.pawelfrydrych.CHAPS.AdapterPlanuProbOnline.podzialPlanu 

而不是

pl.pawelfrydrych.CHAPS.AdapterPlanuProbOffline.podzialPlanu 

Yes, so what should I do? If is there serialization, then I can't get this data in another class?

好的,所以真正的問題不在於序列化。如果您跳過序列化/反序列化步驟,則會出現同樣的問題。

事實是,這兩個嵌套類是無關。你不能在不相關的類之間進行投射。所以,你要麼需要:

  • 只使用一個內部類
  • 轉換爲正確的內部類,或
  • 使相關的內部類(使用通用接口或超EG)和轉換爲普通超類型。

假設內部類是public,您不應該有「訪問」問題。

+0

是的,那我該怎麼辦?如果有序列化,那麼我不能在另一個類中獲取這些數據? – Algeroth 2014-09-29 13:55:13