2013-06-18 27 views
-1

我在做一個應用程序,你可以創建列表,將選擇一個隨機的選擇。在這個應用程序中,我在列表中選擇一個隨機條目。該應用程序將ArrayList序列化爲一個txt文件。我正在將它反序列化ArrayList並打開一個包含該信息的新活動。問題在於它打開該活動時關閉。我相信它與它沒有序列化權利有關。 Anyhelp會很棒,謝謝!的Java:ArrayList的不德/序列化正確

順便說一句,它被取消的原因是被反序列化的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(); 
     } 

    } 
} 

的logcat:

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) 

任何幫助將是真棒!

+0

是那個LogCat? –

+0

是的,它是logcat。 –

+0

已添加LogCat,有何幫助? –

回答

0

方法deserialize不在堆棧跟蹤,因此空列表中的錯誤行FileName

String FileName = textFiles.get(position); 
+0

好吧有道理,但我該怎麼辦才能解決這個問題? –

0

當從文件系統讀取文件,你砍TXT擴展名:

textFiles.add(file.getName().replace(".txt", "")) 

當您單擊您使用的文件名反序列化列表中的項目。原始文件是使用txt擴展名編寫的,所以文件將不會被找到,並且反序列化失敗,列表爲空,List.get(1)將拋出IndexOutOfBoundsException。 你可以做的是:

fis = new FileInputStream(filename+".txt"); 

我敢肯定你logcat的輸出也有IndexOutOfBoundsException異常上方FileNotFoundException異常。

順便說一句,像一個方法命名變量是一個非常糟糕的主意。如果變量名爲textFiles,則使用getTextFiles()作爲方法。

+0

啊哈....很好找,但它得到了同樣的錯誤 –

+0

你可以添加你在反序列化方法中得到的堆棧跟蹤嗎?必須從ex.printStackTrace()中輸出; –