Ocasionally my application would crash, why?
<p>Like the title says ocasionally my application crashes and I am not sure why it does it. </p>

<p>Here is the code: </p>

<p>This is the where I get the input: 
           public class SearchActivity extends Activity {</p>

<pre><code>public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.search);

    final EditText edittext = (EditText) findViewById(R.id.edittext);
    edittext.setOnKeyListener(new OnKeyListener() {
        public boolean onKey(View v, int keyCode, KeyEvent event) {
            // If the event is a key-down event on the "enter" button
            if ((event.getAction() == KeyEvent.ACTION_DOWN)
                    &amp;&amp; (keyCode == KeyEvent.KEYCODE_ENTER)) {
                // Perform action on key press
                String query = edittext.getText().toString();
                // gets the text and makes sure its a string
                Intent intent = new Intent(SearchActivity.this,
                        DissertationActivity.class);
                intent.putExtra("query1", query);
                startActivity(intent);

                return true;
            }
            return false;
        }
    });

    final Button button = (Button) findViewById(R.id.searchButton);
    button.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            String query = edittext.getText().toString();
            // gets the text and makes sure its a string

            Intent intent = new Intent(SearchActivity.this,
                    DissertationActivity.class);
            intent.putExtra("query1", query);
            startActivity(intent);

        }
    });

}
 }
</code></pre>

<p>// Second activity that does the retrieving of the results </p>

<pre><code>         public class DissertationActivity extends ListActivity {
/** Called when the activity is first created. */

public ArrayList&lt;String&gt; book_Array = new ArrayList&lt;String&gt;();
ArrayAdapter&lt;String&gt; adapter;

String href = "";
String href1 = "";
String search_Word = "";

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    Bundle extras = getIntent().getExtras();
    search_Word = extras.getString("query1");

    adapter = new ArrayAdapter&lt;String&gt;(this, R.layout.list_item_1,
            book_Array);
    setListAdapter(adapter);

    new LoadingProgressBar().execute();

}

class LoadingProgressBar extends AsyncTask&lt;String, Integer, String&gt; {

    ProgressDialog pDialog = new ProgressDialog(DissertationActivity.this);

    protected void onPreExecute() {
        this.pDialog.setMessage("Loading");
        this.pDialog.show();

    }

    @Override
    protected String doInBackground(String... params) {

        ListView lv = getListView();
        lv.setTextFilterEnabled(true);

        try {
            Document doc = null;
            Document guestLink = null;

            guestLink = Jsoup.connect("https://aulib.abdn.ac.uk:443/F")
                    .get();
            Element link = guestLink.select("p &gt; a").first();
            href1 = link.attr("href");
            href = href1.substring(0, href1.length() - 2); // removes -0
                                                            // from
                                                            // the

            // href_Array.add(href); //adds href to the array because string
            // wont add to the public var.
            doc = Jsoup.connect(
                    href + "&amp;request=" + search_Word
                            + "&amp;find_code=WRD&amp;adjacent=N&amp;x=0&amp;y=0").get();
            // System.out.println(doc);
            Elements headings = doc.select("td:eq(3)");
            // System.out.println(headings);
            for (Element heading : headings) {
                // System.out.println(heading.text());
                String j = heading.text();

                book_Array.add(j);

            }

        } catch (IOException e) {
            e.printStackTrace();

        }

        book_Array.remove(0);
        book_Array.remove(1);
        book_Array.remove(2);
        book_Array.remove("Search");
        book_Array.remove(" | ");
        book_Array.remove(0);

        pDialog.dismiss();

        lv.setOnItemClickListener(new OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView&lt;?&gt; a, View v, int position,
                    long id) {

                int query = position;

                Intent intent = new Intent(DissertationActivity.this,
                        FullDetailsActivity.class);
                intent.putExtra("href", href);
                intent.putExtra("query1", (int) query);
                intent.putExtra("search_Word", search_Word);

                startActivity(intent);

            }

        });
        return null;
    }

}
</code></pre>

<p>}</p>

<p>Here is the stack trace (sorry I don't know how to format this):</p>

<pre><code>04-26 17:17:20.722: E/WindowManager(8576):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-26 17:19:41.679: E/AndroidRuntime(8608): FATAL EXCEPTION: AsyncTask #5
04-26 17:19:41.679: E/AndroidRuntime(8608): java.lang.RuntimeException: An error occured while executing doInBackground()
04-26 17:19:41.679: E/AndroidRuntime(8608):     at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.lang.Thread.run(Thread.java:1019)
04-26 17:19:41.679: E/AndroidRuntime(8608): Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.ArrayList.throwIndexOutOfBoundsException(ArrayList.java:257)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.ArrayList.remove(ArrayList.java:406)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at com.library_app.DissertationActivity$LoadingProgressBar.doInBackground(DissertationActivity.java:102)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at com.library_app.DissertationActivity$LoadingProgressBar.doInBackground(DissertationActivity.java:1)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-26 17:19:41.679: E/AndroidRuntime(8608):     at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-26 17:19:41.679: E/AndroidRuntime(8608):     ... 4 more
04-26 17:19:41.879: E/WindowManager(8608): Activity com.library_app.DissertationActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407aadc8 that was originally added here
04-26 17:19:41.879: E/WindowManager(8608): android.view.WindowLeaked: Activity com.library_app.DissertationActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@407aadc8 that was originally added here
04-26 17:19:41.879: E/WindowManager(8608):  at android.view.ViewRoot.&lt;init&gt;(ViewRoot.java:259)
04-26 17:19:41.879: E/WindowManager(8608):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-26 17:19:41.879: E/WindowManager(8608):  at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-26 17:19:41.879: E/WindowManager(8608):  at android.view.Window$LocalWindowManager.addView(Window.java:465)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.Dialog.show(Dialog.java:241)
04-26 17:19:41.879: E/WindowManager(8608):  at com.library_app.DissertationActivity$LoadingProgressBar.onPreExecute(DissertationActivity.java:59)
04-26 17:19:41.879: E/WindowManager(8608):  at android.os.AsyncTask.execute(AsyncTask.java:391)
04-26 17:19:41.879: E/WindowManager(8608):  at com.library_app.DissertationActivity.onCreate(DissertationActivity.java:49)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread.handleRelaunchActivity(ActivityThread.java:2953)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread.access$1600(ActivityThread.java:123)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread$H.handleMessage(ActivityThread.java:943)
04-26 17:19:41.879: E/WindowManager(8608):  at android.os.Handler.dispatchMessage(Handler.java:99)
04-26 17:19:41.879: E/WindowManager(8608):  at android.os.Looper.loop(Looper.java:130)
04-26 17:19:41.879: E/WindowManager(8608):  at android.app.ActivityThread.main(ActivityThread.java:3835)
04-26 17:19:41.879: E/WindowManager(8608):  at java.lang.reflect.Method.invokeNative(Native Method)
04-26 17:19:41.879: E/WindowManager(8608):  at java.lang.reflect.Method.invoke(Method.java:507)
04-26 17:19:41.879: E/WindowManager(8608):  at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-26 17:19:41.879: E/WindowManager(8608):  at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-26 17:19:41.879: E/WindowManager(8608):  at dalvik.system.NativeStart.main(Native Method)
</code></pre>

<p>It will only crash sometimes. It is always after I search for something. I have also noticed that if I go back using the button on the phone and try to search for something again it crashes, not all times but only some times. </p>

<p>What would be causing the crash? How would I fix it?</p>

<p>Thanks!</p>
