タイトルにあるように、アプリケーションがクラッシュすることがありますが、なぜクラッシュするのかわかりません。
コードは次のとおりです。
これが私が入力を得るところです:public class SearchActivity extends 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);
}
});
}
}
//結果の取得を行う2番目のアクティビティ
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)
たまにしかクラッシュしません。いつも何かを探した後です。また、電話のボタンを使用して戻ってもう一度検索しようとすると、クラッシュすることがありますが、常にではなく、一部の場合にのみクラッシュします。
クラッシュの原因は何ですか?どうすれば修正できますか?
ありがとう!