我如何修复IllegalStateException?

时间:2015-03-28 21:04:29

标签: java android eclipse file-io exception-handling

当我运行我的应用程序并运行我的选项10(NewBoatListActivity)时,应用程序崩溃了。根据日志,它是由java.lang.IllegalStateException引起的。我有一种感觉,它是由if else语句中的某个地方引起的。如果有人知道如何解决这个问题,请查看下面的代码:

public void createList(View view)
{
    //Declare references
    BoatList boat_list;
    Boat newBoat;
    Boat newBoat2;
    Boat newBoat3;
    TextView tv;
    EditText et;
    EditText et2;
    String infilename;
    String urlfile;
    boolean file_problem = false;
    Scanner fsc = null;

    //Set references
    et = (EditText) findViewById(R.id.edit_infile);

    et2 = (EditText) findViewById(R.id.edit_urlfile);

    tv = (TextView) findViewById(R.id.text_main);

    //Get user input. Allows user to choose which way to get the file
    infilename = et.getText().toString();
    urlfile = et2.getText().toString();

    //Create the new boat object
    newBoat = new Boat();
    newBoat2 = new Boat();
    newBoat3 = new Boat();

    //Try to access and open the file in the app's assets directory
    //AssetManager assetManager = getAssets();
    try
    {
        AssetManager assetManager = getAssets();
        fsc = new Scanner(assetManager.open(infilename));

        //Show the file was successfully opened and read
        tv.setText("Infile was opened and read successfully!");

        //Close the file
        fsc.close();
    }
    catch(IOException e)
    {
        tv.setText("Error: File does not exist!");
        file_problem = true;
    }

    //If the file does exist in the assets directory, then read the data and 
    //input the data values to the Boat object
    if(!file_problem)
    {
        String make = fsc.nextLine();
        newBoat.setMake(make);

        String regist = fsc.nextLine();
        newBoat.setRegi(regist);

        int length = fsc.nextInt();
        fsc.nextLine();
        newBoat.setLength(length);

        int beam = fsc.nextInt();
        fsc.nextLine();
        newBoat.setBeam(beam);

        String fuel = fsc.nextLine();
        newBoat.setFuel(fuel);

        double price = fsc.nextDouble();
        fsc.nextLine();
        newBoat.setPrice(price);

        String pic_url = fsc.nextLine();
        newBoat.setPicURL(pic_url);

        String make2 = fsc.nextLine();
        newBoat2.setMake(make2);

        String regist2 = fsc.nextLine();
        newBoat2.setRegi(regist2);

        int length2 = fsc.nextInt();
        fsc.nextLine();
        newBoat2.setLength(length2);

        int beam2 = fsc.nextInt();
        fsc.nextLine();
        newBoat2.setBeam(beam2);

        String fuel2 = fsc.nextLine();
        newBoat2.setFuel(fuel2);

        double price2 = fsc.nextDouble();
        fsc.nextLine();
        newBoat2.setPrice(price2);

        String pic_url2 = fsc.nextLine();
        newBoat2.setPicURL(pic_url2);

        String make3 = fsc.nextLine();
        newBoat3.setMake(make3);

        String regist3 = fsc.nextLine();
        newBoat3.setRegi(regist3);

        int length3 = fsc.nextInt();
        fsc.nextLine();
        newBoat3.setLength(length3);

        int beam3 = fsc.nextInt();
        fsc.nextLine();
        newBoat3.setBeam(beam3);

        String fuel3 = fsc.nextLine();
        newBoat3.setFuel(fuel3);

        double price3 = fsc.nextDouble();
        fsc.nextLine();
        newBoat3.setPrice(price3);

        String pic_url3 = fsc.nextLine();
        newBoat3.setPicURL(pic_url3);

        //Close the file
        fsc.close();
    }

    //Access the list
    boat_list = BoatList.getInstance();

    //Erase all existing items in the BoatList and add new ones
    boat_list.clear();

    if(boat_list.isEmpty())
    {
        boat_list.add(boat_list.size(), newBoat);
        boat_list.add(boat_list.size(), newBoat2);
        boat_list.add(boat_list.size(), newBoat3);
    }

} //end of createList

1 个答案:

答案 0 :(得分:1)

您正在阅读已关闭的文件。在完成之前不要关闭文件。

另外,将来发布完整的堆栈跟踪。它提供了大量信息,使调试更简单 - 行号和消息。