我正在制作一个应用程序,您可以在其中创建将随机选择的列表。在这个应用程序中,我在列表中选择了一个随机条目。该应用程序将 ArrayList 序列化为 txt 文件。我正在制作反序列化 ArrayList 并使用该信息打开一个新活动的部分。问题是它禁止打开该活动。我相信这与它没有正确序列化有关。Anyhelp会很棒,谢谢!
顺便说一句,它取消的原因是反序列化的 ArrayList 是空的。
这是第一个 Java 文件:
package com.frostbytedev.randomgenie;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Steven on 6/11/13.
*/
public class NewList extends Activity implements Serializable, View.OnClickListener{
String ListName;
String ItemText;
int i = 0;
List<String> List = new ArrayList<String>();
Button save;
EditText FileName, etItem1, etItem2, etItem3, etItem4, etItem5;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.newlist);
initilize();
}
private void IndexList() {
List.add(FileName.getText().toString());
List.add(etItem1.getText().toString());
List.add(etItem2.getText().toString());
List.add(etItem3.getText().toString());
List.add(etItem4.getText().toString());
List.add(etItem5.getText().toString());
for(i=1;i<5;i++){
ItemText = List.get(i);
if(ItemText.contentEquals("")){
List.remove(List.get(i));
}
}
}
private void initilize() {
save = (Button)findViewById(R.id.bSave);
FileName = (EditText)findViewById(R.id.etFileName);
etItem1 = (EditText)findViewById(R.id.etItem1);
etItem2 = (EditText)findViewById(R.id.etItem2);
etItem3 = (EditText)findViewById(R.id.etItem3);
etItem4 = (EditText)findViewById(R.id.etItem4);
etItem5 = (EditText)findViewById(R.id.etItem5);
save.setOnClickListener(this);
}
@Override
public void onClick(View view) {
switch(view.getId()){
case R.id.bSave:
IndexList();
try {
SaveList();
} catch (IOException e) {
e.printStackTrace();
}
}
}
private void SaveList() throws IOException {
String filename = FileName.getText().toString()+".txt";
FileOutputStream fos;
try {
fos = openFileOutput(filename,Context.MODE_PRIVATE);
ObjectOutputStream out = new ObjectOutputStream(fos);
out.writeObject(List);
out.close();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
第二个:
package com.frostbytedev.randomgenie;
import android.app.Activity;
import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.ListView;
import java.io.*;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Steven on 6/15/13.
*/
public class ListSelect extends ListActivity implements Serializable {
List<String> List = new ArrayList<String>();
List<String> textFiles = new ArrayList<String>();
List<String> ListStrings = new ArrayList<String>();
List<String> textFiles(String directory) {
File dir = new File(directory);
for (File file : dir.listFiles()) {
if (file.getName().endsWith((".txt"))) {
textFiles.add(file.getName().replace(".txt", ""));
}
}
return textFiles;
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, textFiles("data/data/com.frostbytedev.randomgenie/files")));
}
protected void onListItemClick(ListView l, View v, int position, long id) {
super.onListItemClick(l, v, position, id);
String FileName = textFiles.get(position);
deserialize(FileName);
Bundle b = new Bundle();
Intent OpenList = new Intent(this, ListRandom.class);
OpenList.putExtra("ListItem1",List.get(1));
OpenList.putExtra("ListItem2", List.get(2));
OpenList.putExtra("ListItem3", List.get(3));
OpenList.putExtra("ListItem4",List.get(4));
OpenList.putExtra("ListItem5", List.get(5));
startActivity(OpenList);
}
private void deserialize(String filename) {
FileInputStream fis = null;
ObjectInputStream in = null;
ObjectOutputStream out = null;
try {
fis = new FileInputStream(filename);
in = new ObjectInputStream(fis);
List = (ArrayList<String>) in.readObject();
out.close();
} catch (Exception ex) {
ex.printStackTrace();
}
}
}
日志猫:
06-19 00:00:53.497 8447-8447/com.frostbytedev.randomgenie E/AndroidRuntime: FATAL EXCEPTION: main
java.lang.IndexOutOfBoundsException: Invalid index 1, size is 0
at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:251)
at java.util.ArrayList.get(ArrayList.java:304)
at com.frostbytedev.randomgenie.ListSelect.onListItemClick(ListSelect.java:44)
at android.app.ListActivity$2.onItemClick(ListActivity.java:319)
at android.widget.AdapterView.performItemClick(AdapterView.java:298)
at android.widget.AbsListView.performItemClick(AbsListView.java:1100)
at android.widget.AbsListView$PerformClick.run(AbsListView.java:2749)
at android.widget.AbsListView$1.run(AbsListView.java:3423)
at android.os.Handler.handleCallback(Handler.java:725)
at android.os.Handler.dispatchMessage(Handler.java:92)
at android.os.Looper.loop(Looper.java:137)
at android.app.ActivityThread.main(ActivityThread.java:5041)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:793)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:560)
at dalvik.system.NativeStart.main(Native Method)
任何帮助都是极好的!