- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有两个类。在第一个类中,我从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
,您不应该遇到“访问”问题。
关于java - ObjectInputStream 无法正确转换对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26101132/
当我尝试模拟 ObjectInputStream 对象时,我得到一个 NullPointerException。更准确地说,当调用此行时: when(inputStream.readObject())
这是我的代码: ObjectInputStream ois = null; UserRegistration UR = new UserRegistration(); Scan
所以我试图将一个对象从客户端发送到服务器,然后我用这个对象做一些事情。我验证了发送时对象包含数据。当我在调试器中读取对象时,它的所有属性均为 null。 下面是我发送对象的位置: private cl
我正在使用套接字对java网络进行编程,并使用简单对象在远程程序之间交换数据。 在本例中,我有一个使用以下方法构造的ObjectInputStream: new ObjectInputStream(S
我一直在尝试制作一款两人多米诺骨牌游戏。目前,我希望在玩家 2 的屏幕上模仿玩家 1 的 Action ,仅此而已。如果我启动玩家 1 的屏幕并移动一些多米诺骨牌,则当我启动玩家 2 的屏幕时会出现更
这个问题已经有答案了: Sending the same but modifed object over ObjectOutputStream (2 个回答) 已关闭 4 年前。 我正在尝试通过套接字
我在一个文件中存储了多个对象。这与 ObjectInputStream 有关。如果我有以下代码: FileInputStream fis = new FileInputStream(filename)
这个问题已经有答案了: Java socket/serialization, object won't update (2 个回答) 已关闭 8 年前。 我有多个客户端和一台服务器。服务器在一个线程中
我有一系列通过网络套接字发送的对象。 用于读取数据的代码已经实现并且可以工作,但我的问题是有时套接字超时会中断对象的读取(ObjectInputStream.readObject()),从而损坏流.
当我尝试从服务器发送一个对象时,我的计算机似乎崩溃了,第 27 行尝试接收该对象,我想我得到了一个无效的类型代码:0一次,但通常只是崩溃,所以我必须重新启动 new Thread(
我试图在第24行的客户端中实例化一个ObjectInputStream,但它不会,我可以实例化一个ObjectOutputStream,但不能实例化Input。 private class C
我正在尝试使用以下代码读取我保存的文件: public void saveOnFile() { try { ObjectOutputStream output = ne
我保存了一个ArrayList作为带有 ObjectOutputStream 的对象。现在,我如何在另一个应用程序项目上使用 ObjectInputStream 读取该对象?我尝试了一下,它抛出了 C
我有两个类。在第一个类中,我从List中写入数据,在第二堂课中,我想读取这些数据并将其放入listview中。它正确保存数据,但是当我尝试获取它时,logcat 给出错误: java.lang.Cla
我正在尝试将 ArrayList 发送到 Android 设备上的客户端。服务器说它发送了该对象,但是在 Android 设备上它挂起。我已经阅读过,在创建 ObjectInputStream 时,必
我正在通过 java 套接字将文件从 Google glass 设备读取到 PC。由于 catch 语句抛出 eof 异常,我的其余代码被忽略。我该如何解决这个问题? 代码:接收图像(并将其放入简单的
几天前,我开始编写一个小型多人游戏,其中我使用 ObjectInputStreams 和 ObjectOutputStreams 与服务器交换数据。但由于某种原因,服务器没有收到任何东西。所以我寻求一
我有一个套接字,它每隔几秒通过 ObjectOutputStream 向客户端发送一个对象列表。在服务器端,每次 writeObject(myList) 之后,我执行 flush 然后 reset。使
这个问题已经有答案了: ObjectInput and Output streams are not being accepted by client/server (2 个回答) 已关闭 5 年前。
我在使用二进制文件加载应用程序时遇到问题。我正在尝试从二进制文件中读取数据并将数据注入(inject)到我的应用程序中的一些 HashMaps/ArrayLists 中。 public void lo
我是一名优秀的程序员,十分优秀!