ObjectInputStream无法正确转换对象

时间:2014-09-29 13:30:21

标签: java android

我有2节课。在第一节课中,我从List中写入数据,在第二节课中,我想读取这些数据并放入listview。它正确保存数据,但是当我尝试获取数据时,logcat会出错:

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 个答案:

答案 0 :(得分: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

  

是的,那我该怎么办?如果有序列化,那么我无法在另一个类中获取这些数据吗?

好的,所以真正的问题不是序列化。如果跳过序列化/反序列化步骤,则会遇到同样的问题。

事实是这两个嵌套类是无关的。你不能在不相关的类之间施放。所以你要么:

  • 只使用一个内部类
  • 施放到正确的内部类,或
  • 使内部类相关(例如使用公共接口或超类)并转换为公共超类型。

假设内部类是public,你不应该访问&#34;&#34;问题。