gpt4 book ai didi

java - ObjectInputStream 无法正确转换对象

转载 作者:行者123 更新时间:2023-12-01 12:29:01 25 4
gpt4 key购买 nike

我有两个类。在第一个类中,我从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;
}

最佳答案

好吧,让我们看看异常消息:

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?

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

事实上,这两个嵌套类不相关。您不能在不相关的类之间进行转换。所以你要么需要:

  • 仅使用一个内部类
  • 强制转换为正确的内部类,或者
  • 使内部类相关(例如使用公共(public)接口(interface)或父类(super class))并强制转换为公共(public)父类(super class)型。

假设内部类是public ,您不应该遇到“访问”问题。

关于java - ObjectInputStream 无法正确转换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101132/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com