偶尔我的应用程序会崩溃,为什么?

时间:2012-04-26 16:30:48

标签: android

就像标题说我的应用程序崩溃了,我不知道为什么会这样做。

以下是代码:

这是我获得输入的地方:            公共类SearchActivity扩展了Activity {

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)
                    && (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);

        }
    });

}
 }

//检索结果的第二个活动

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

public ArrayList<String> book_Array = new ArrayList<String>();
ArrayAdapter<String> 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<String>(this, R.layout.list_item_1,
            book_Array);
    setListAdapter(adapter);

    new LoadingProgressBar().execute();

}

class LoadingProgressBar extends AsyncTask<String, Integer, String> {

    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 > 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 + "&request=" + search_Word
                            + "&find_code=WRD&adjacent=N&x=0&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<?> 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;
    }

}

}

这是堆栈跟踪(抱歉,我不知道如何格式化):

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.<init>(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)

它有时会崩溃。它总是在我搜索之后。我还注意到,如果我回去使用手机上的按钮并尝试再次搜索它,它会崩溃,而不是所有时间都会崩溃。

导致坠机的原因是什么?我该如何解决?

谢谢!

1 个答案:

答案 0 :(得分:4)

"Caused by: java.lang.IndexOutOfBoundsException: Invalid index 0, size is 0"

显示您正在尝试访问大小为0(空)

的数组
"at java.util.ArrayList.remove"

告诉我,这是由于您尝试从阵列中删除项目造成的。

可能,其中一条线:

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

导致它,因为你没有在任何地方检查以确保你的book_array实际上有这些值。

at com.library_app.DissertationActivity$LoadingProgressBar.doInBackground(DissertationActivity.java:102)

为您提供导致异常的确切行,以便您可以检查该行并确保在执行之前验证您的值。 (在logcat中双击该行,如果您正在使用Eclipse,它将带您进入该行代码)