I am trying to create a progress dialog in between two intents. Here is the code:
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 LoginProgressTask().execute();
}
class LoginProgressTask extends AsyncTask<String, Integer, String> {
ProgressDialog pDialog = new ProgressDialog(DissertationActivity.this);
protected void onPreExecute() {
this.pDialog.setMessage("Progress start");
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);
adapter.notifyDataSetChanged();
book_Array.remove(1);
adapter.notifyDataSetChanged();
book_Array.remove(2);
adapter.notifyDataSetChanged();
book_Array.remove("Search");
adapter.notifyDataSetChanged();
book_Array.remove(" | ");
adapter.notifyDataSetChanged();
book_Array.remove(0);
adapter.notifyDataSetChanged();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> a, View v, int position,
long id) {
// Context context = getApplicationContext();
int query = position;
// String text = book_Array.get(position);
// int duration = Toast.LENGTH_SHORT;
// Toast toast = Toast.makeText(context,
// String.valueOf(position), //shows the postion in the
// array
// list
// duration);
// toast.show();
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;
}
protected void onPostExecute(String...params) {
pDialog.dismiss();
}
}}
04-23 17:45:16.752: E/AndroidRuntime(2425): FATAL EXCEPTION: AsyncTask #1
04-23 17:45:16.752: E/AndroidRuntime(2425): java.lang.RuntimeException: An error occured while executing doInBackground()
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.os.AsyncTask$3.done(AsyncTask.java:200)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:274)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.FutureTask.setException(FutureTask.java:125)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:308)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.FutureTask.run(FutureTask.java:138)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1088)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:581)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.lang.Thread.run(Thread.java:1019)
04-23 17:45:16.752: E/AndroidRuntime(2425): Caused by: android.view.ViewRoot$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.ViewRoot.checkThread(ViewRoot.java:2941)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.ViewRoot.focusableViewAvailable(ViewRoot.java:1717)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.ViewGroup.focusableViewAvailable(ViewGroup.java:452)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.View.setFlags(View.java:4631)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.view.View.setFocusableInTouchMode(View.java:3206)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.widget.AdapterView.checkFocus(AdapterView.java:694)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.widget.AdapterView$AdapterDataSetObserver.onChanged(AdapterView.java:789)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.database.DataSetObservable.notifyChanged(DataSetObservable.java:31)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.widget.BaseAdapter.notifyDataSetChanged(BaseAdapter.java:50)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.widget.ArrayAdapter.notifyDataSetChanged(ArrayAdapter.java:247)
04-23 17:45:16.752: E/AndroidRuntime(2425): at com.library_app.DissertationActivity$LoginProgressTask.doInBackground(DissertationActivity.java:103)
04-23 17:45:16.752: E/AndroidRuntime(2425): at com.library_app.DissertationActivity$LoginProgressTask.doInBackground(DissertationActivity.java:1)
04-23 17:45:16.752: E/AndroidRuntime(2425): at android.os.AsyncTask$2.call(AsyncTask.java:185)
04-23 17:45:16.752: E/AndroidRuntime(2425): at java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:306)
04-23 17:45:16.752: E/AndroidRuntime(2425): … 4 more
04-23 17:45:16.902: E/WindowManager(2425): Activity com.library_app.DissertationActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40666e70 that was originally added here
04-23 17:45:16.902: E/WindowManager(2425): android.view.WindowLeaked: Activity com.library_app.DissertationActivity has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView@40666e70 that was originally added here
04-23 17:45:16.902: E/WindowManager(2425): at android.view.ViewRoot.(ViewRoot.java:259)
04-23 17:45:16.902: E/WindowManager(2425): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:148)
04-23 17:45:16.902: E/WindowManager(2425): at android.view.WindowManagerImpl.addView(WindowManagerImpl.java:91)
04-23 17:45:16.902: E/WindowManager(2425): at android.view.Window$LocalWindowManager.addView(Window.java:465)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.Dialog.show(Dialog.java:241)
04-23 17:45:16.902: E/WindowManager(2425): at com.library_app.DissertationActivity$LoginProgressTask.onPreExecute(DissertationActivity.java:59)
04-23 17:45:16.902: E/WindowManager(2425): at android.os.AsyncTask.execute(AsyncTask.java:391)
04-23 17:45:16.902: E/WindowManager(2425): at com.library_app.DissertationActivity.onCreate(DissertationActivity.java:49)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1047)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:1722)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:1784)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.ActivityThread.access$1500(ActivityThread.java:123)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:939)
04-23 17:45:16.902: E/WindowManager(2425): at android.os.Handler.dispatchMessage(Handler.java:99)
04-23 17:45:16.902: E/WindowManager(2425): at android.os.Looper.loop(Looper.java:130)
04-23 17:45:16.902: E/WindowManager(2425): at android.app.ActivityThread.main(ActivityThread.java:3835)
04-23 17:45:16.902: E/WindowManager(2425): at java.lang.reflect.Method.invokeNative(Native Method)
04-23 17:45:16.902: E/WindowManager(2425): at java.lang.reflect.Method.invoke(Method.java:507)
04-23 17:45:16.902: E/WindowManager(2425): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:847)
04-23 17:45:16.902: E/WindowManager(2425): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:605)
04-23 17:45:16.902: E/WindowManager(2425): at dalvik.system.NativeStart.main(Native Method)
[sorry don’t know how to indent this]
The progress bar manages to load but the code crashes after a few seconds. Where should the intents be, in the doInBackground or in postExecute? If the intents are meant to be in postExecte how would I pass the variables from doInBackground to postExecute?
What is wrong with the code and what needs to be changed so that it doesn’t crash anymore?
You can update
UIindoInBackGround()Seeing you log trace, I can tell that you are trying to update UI in doInBackGround which you cannot do directly instead call runOnUiThread method and everything will be fine…
Use