I’m trying to use Progress Dialog with Async task in my Adroid app. But when I run my app this is causing a Fatal Exception. When I used it in another method it worked properly. But when I used it with the method below, it caused an error. I read in other questions that it’s because of the Array, but I don’t know whether it is true.
Here is my Activity codes:
private class TheTask extends AsyncTask<Void, Void, Void>{
@Override
protected void onPreExecute() {
authProgressDialog = ProgressDialog.show(Front9Activity.this, "", "Authenticating...", true, false);
}
@Override
protected Void doInBackground(Void... params) {
loading();
return null;
}
@Override
protected void onPostExecute(Void result) {
if(authProgressDialog.isShowing()) {
authProgressDialog.dismiss();
}
return;
}
}
public void loading(){
mode = Integer.parseInt(gametype);
check = Integer.parseInt(type);
if(mode == 1) {
name = name1;
number = mobile1;
} else if(mode == 2) {
name = name2;
number = mobile2;
} else if(mode == 3) {
name = name3;
number = mobile3;
} else {
name = name4;
number = mobile4;
}
if(check == 1){
data = "Solo";
} else if(check == 2) {
data = "GroupScore";
} else {
data = "Tournament";
}
save = openOrCreateDatabase("clubscores", MODE_PRIVATE,null);
Cursor c = save.rawQuery("Select *from Courses where VenueName = '"+ courses +"'", null);
c.moveToFirst();
((TextView) findViewById(R.id.front9hole1)).setText(c.getString(c.getColumnIndex("Hole1")));
((TextView) findViewById(R.id.front9hole2)).setText(c.getString(c.getColumnIndex("Hole2")));
((TextView) findViewById(R.id.front9hole3)).setText(c.getString(c.getColumnIndex("Hole3")));
((TextView) findViewById(R.id.front9hole4)).setText(c.getString(c.getColumnIndex("Hole4")));
((TextView) findViewById(R.id.front9hole5)).setText(c.getString(c.getColumnIndex("Hole5")));
((TextView) findViewById(R.id.front9hole6)).setText(c.getString(c.getColumnIndex("Hole6")));
((TextView) findViewById(R.id.front9hole7)).setText(c.getString(c.getColumnIndex("Hole7")));
((TextView) findViewById(R.id.front9hole8)).setText(c.getString(c.getColumnIndex("Hole8")));
((TextView) findViewById(R.id.front9hole9)).setText(c.getString(c.getColumnIndex("Hole9")));
((TextView) findViewById(R.id.front9hole10)).setText(c.getString(c.getColumnIndex("Hole10")));
((TextView) findViewById(R.id.front9hole11)).setText(c.getString(c.getColumnIndex("Hole11")));
((TextView) findViewById(R.id.front9hole12)).setText(c.getString(c.getColumnIndex("Hole12")));
((TextView) findViewById(R.id.front9hole13)).setText(c.getString(c.getColumnIndex("Hole13")));
((TextView) findViewById(R.id.front9hole14)).setText(c.getString(c.getColumnIndex("Hole14")));
((TextView) findViewById(R.id.front9hole15)).setText(c.getString(c.getColumnIndex("Hole15")));
((TextView) findViewById(R.id.front9hole16)).setText(c.getString(c.getColumnIndex("Hole16")));
((TextView) findViewById(R.id.front9hole17)).setText(c.getString(c.getColumnIndex("Hole17")));
((TextView) findViewById(R.id.front9hole18)).setText(c.getString(c.getColumnIndex("Hole18")));
p1 = Integer.parseInt(iden);
Cursor c3 = save.rawQuery("Select *from " +data + " where Venue = '"+ courses +"'", null);
c3.moveToFirst();
int places = c3.getCount();
if(places == 0){
} else {
if(p1 == 111){
Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
count = cc.getCount();
if(count == 0){
} else {
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
hole2 = cc.getString(cc.getColumnIndex("Hole2"));
hole3 = cc.getString(cc.getColumnIndex("Hole3"));
hole4 = cc.getString(cc.getColumnIndex("Hole4"));
hole5 = cc.getString(cc.getColumnIndex("Hole5"));
hole6 = cc.getString(cc.getColumnIndex("Hole6"));
hole7 = cc.getString(cc.getColumnIndex("Hole7"));
hole8 = cc.getString(cc.getColumnIndex("Hole8"));
hole9 = cc.getString(cc.getColumnIndex("Hole9"));
hole10 = cc.getString(cc.getColumnIndex("Hole10"));
hole11 = cc.getString(cc.getColumnIndex("Hole11"));
hole12 = cc.getString(cc.getColumnIndex("Hole12"));
hole13 = cc.getString(cc.getColumnIndex("Hole13"));
hole14 = cc.getString(cc.getColumnIndex("Hole14"));
hole15 = cc.getString(cc.getColumnIndex("Hole15"));
hole16 = cc.getString(cc.getColumnIndex("Hole16"));
hole17 = cc.getString(cc.getColumnIndex("Hole17"));
hole18 = cc.getString(cc.getColumnIndex("Hole18"));
}
} else if(p1 == 222) {
Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
count = cc.getCount();
if(count == 0){
} else {
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
hole2 = cc.getString(cc.getColumnIndex("Hole2"));
hole3 = cc.getString(cc.getColumnIndex("Hole3"));
hole4 = cc.getString(cc.getColumnIndex("Hole4"));
hole5 = cc.getString(cc.getColumnIndex("Hole5"));
hole6 = cc.getString(cc.getColumnIndex("Hole6"));
hole7 = cc.getString(cc.getColumnIndex("Hole7"));
hole8 = cc.getString(cc.getColumnIndex("Hole8"));
hole9 = cc.getString(cc.getColumnIndex("Hole9"));
hole10 = cc.getString(cc.getColumnIndex("Hole10"));
hole11 = cc.getString(cc.getColumnIndex("Hole11"));
hole12 = cc.getString(cc.getColumnIndex("Hole12"));
hole13 = cc.getString(cc.getColumnIndex("Hole13"));
hole14 = cc.getString(cc.getColumnIndex("Hole14"));
hole15 = cc.getString(cc.getColumnIndex("Hole15"));
hole16 = cc.getString(cc.getColumnIndex("Hole16"));
hole17 = cc.getString(cc.getColumnIndex("Hole17"));
hole18 = cc.getString(cc.getColumnIndex("Hole18"));
}
} else if(p1 == 333) {
Cursor cc = save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
count = cc.getCount();
if(count == 0){
}else{
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
hole2 = cc.getString(cc.getColumnIndex("Hole2"));
hole3 = cc.getString(cc.getColumnIndex("Hole3"));
hole4 = cc.getString(cc.getColumnIndex("Hole4"));
hole5 = cc.getString(cc.getColumnIndex("Hole5"));
hole6 = cc.getString(cc.getColumnIndex("Hole6"));
hole7 = cc.getString(cc.getColumnIndex("Hole7"));
hole8 = cc.getString(cc.getColumnIndex("Hole8"));
hole9 = cc.getString(cc.getColumnIndex("Hole9"));
hole10 = cc.getString(cc.getColumnIndex("Hole10"));
hole11 = cc.getString(cc.getColumnIndex("Hole11"));
hole12 = cc.getString(cc.getColumnIndex("Hole12"));
hole13 = cc.getString(cc.getColumnIndex("Hole13"));
hole14 = cc.getString(cc.getColumnIndex("Hole14"));
hole15 = cc.getString(cc.getColumnIndex("Hole15"));
hole16 = cc.getString(cc.getColumnIndex("Hole16"));
hole17 = cc.getString(cc.getColumnIndex("Hole17"));
hole18 = cc.getString(cc.getColumnIndex("Hole18"));
}
} else if(p1 == 444) {
Cursor cc =save.rawQuery("Select *from " +data + " where Mobile = '"+ number +"'", null);
cc.moveToFirst();
count = cc.getCount();
if(count == 0){
}else{
hole1 = cc.getString(cc.getColumnIndex("Hole1"));
hole2 = cc.getString(cc.getColumnIndex("Hole2"));
hole3 = cc.getString(cc.getColumnIndex("Hole3"));
hole4 = cc.getString(cc.getColumnIndex("Hole4"));
hole5 = cc.getString(cc.getColumnIndex("Hole5"));
hole6 = cc.getString(cc.getColumnIndex("Hole6"));
hole7 = cc.getString(cc.getColumnIndex("Hole7"));
hole8 = cc.getString(cc.getColumnIndex("Hole8"));
hole9 = cc.getString(cc.getColumnIndex("Hole9"));
hole10 = cc.getString(cc.getColumnIndex("Hole10"));
hole11 = cc.getString(cc.getColumnIndex("Hole11"));
hole12 = cc.getString(cc.getColumnIndex("Hole12"));
hole13 = cc.getString(cc.getColumnIndex("Hole13"));
hole14 = cc.getString(cc.getColumnIndex("Hole14"));
hole15 = cc.getString(cc.getColumnIndex("Hole15"));
hole16 = cc.getString(cc.getColumnIndex("Hole16"));
hole17 = cc.getString(cc.getColumnIndex("Hole17"));
hole18 = cc.getString(cc.getColumnIndex("Hole18"));
}
}
}
ArrayAdapter<String> adapter1 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole1,"1", "2", "3", "4","5","6","7","8","9" });
adapter1.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter2 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole2,"1", "2", "3", "4","5","6","7","8","9" });
adapter2.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter3 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole3,"1", "2", "3", "4","5","6","7","8","9" });
adapter3.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter4 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole4,"1", "2", "3", "4","5","6","7","8","9" });
adapter4.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter5 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole5,"1", "2", "3", "4","5","6","7","8","9" });
adapter5.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter6 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole6,"1", "2", "3", "4","5","6","7","8","9" });
adapter6.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter7 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole7,"1", "2", "3", "4","5","6","7","8","9" });
adapter7.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter8 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole8,"1", "2", "3", "4","5","6","7","8","9" });
adapter8.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter9 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole9,"1", "2", "3", "4","5","6","7","8","9" });
adapter9.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter10 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole10,"1", "2", "3", "4","5","6","7","8","9" });
adapter10.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter11 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole11,"1", "2", "3", "4","5","6","7","8","9" });
adapter11.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter12 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole12,"1", "2", "3", "4","5","6","7","8","9" });
adapter12.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter13 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole13,"1", "2", "3", "4","5","6","7","8","9" });
adapter13.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter14 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole14,"1", "2", "3", "4","5","6","7","8","9" });
adapter14.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter15 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole15,"1", "2", "3", "4","5","6","7","8","9" });
adapter15.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter16 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole16,"1", "2", "3", "4","5","6","7","8","9" });
adapter16.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter17 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole17,"1", "2", "3", "4","5","6","7","8","9" });
adapter17.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
ArrayAdapter<String> adapter18 = new ArrayAdapter<String>(
this,
R.layout.spinnerlayout,
new String[] { hole18,"1", "2", "3", "4","5","6","7","8","9" });
adapter18.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
Spinner spinner = (Spinner)this.findViewById(R.id.soloscorehole1front9);
spinner.setAdapter(adapter1);
((Spinner) findViewById(R.id.soloscorehole2front9)).setAdapter(adapter2);
((Spinner) findViewById(R.id.soloscorehole3front9)).setAdapter(adapter3);
((Spinner) findViewById(R.id.soloscorehole4front9)).setAdapter(adapter4);
((Spinner) findViewById(R.id.soloscorehole5front9)).setAdapter(adapter5);
((Spinner) findViewById(R.id.soloscorehole6front9)).setAdapter(adapter6);
((Spinner) findViewById(R.id.soloscorehole7front9)).setAdapter(adapter7);
((Spinner) findViewById(R.id.soloscorehole8front9)).setAdapter(adapter8);
((Spinner) findViewById(R.id.soloscorehole9front9)).setAdapter(adapter9);
((Spinner) findViewById(R.id.soloscorehole10front9)).setAdapter(adapter10);
((Spinner) findViewById(R.id.soloscorehole11front9)).setAdapter(adapter11);
((Spinner) findViewById(R.id.soloscorehole12front9)).setAdapter(adapter12);
((Spinner) findViewById(R.id.soloscorehole13front9)).setAdapter(adapter13);
((Spinner) findViewById(R.id.soloscorehole14front9)).setAdapter(adapter14);
((Spinner) findViewById(R.id.soloscorehole15front9)).setAdapter(adapter15);
((Spinner) findViewById(R.id.soloscorehole16front9)).setAdapter(adapter16);
((Spinner) findViewById(R.id.soloscorehole17front9)).setAdapter(adapter17);
((Spinner) findViewById(R.id.soloscorehole18front9)).setAdapter(adapter18);
((Spinner) findViewById(R.id.soloscorehole2front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole3front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole4front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole5front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole6front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole7front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole8front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole9front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole10front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole11front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole12front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole13front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole14front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole15front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole16front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole17front9)).setEnabled(false);
((Spinner) findViewById(R.id.soloscorehole18front9)).setEnabled(false);
}
This is what logcat says:
01-20 17:16:54.742: E/AndroidRuntime(28665): FATAL EXCEPTION: AsyncTask #1
01-20 17:16:54.742: E/AndroidRuntime(28665): java.lang.RuntimeException: An error occured while executing doInBackground()
01-20 17:16:54.742: E/AndroidRuntime(28665): at
android.os.AsyncTask$3.done(AsyncTask.java:299)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.FutureTask.setException(FutureTask.java:124)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.FutureTask$Sync.innerRun(FutureTask.java:307)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.FutureTask.run(FutureTask.java:137)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1076)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:569)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
java.lang.Thread.run(Thread.java:856)
01-20 17:16:54.742: E/AndroidRuntime(28665): Caused by:
android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
01-20 17:16:54.742: E/AndroidRuntime(28665): at
android.view.ViewRootImpl.checkThread(ViewRootImpl.java:4607)
01-20 17:16:54.742: E/AndroidRuntime(28665): at
android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:835)
You’re getting an error because you’re calling
loading()from a background thread indoInBackground(). Inloading(), you’re modifying aspects of the UI. This is not allowed on any thread except the Main UI thread, and you get an exception because you do it from the background thread.You should ideally be doing all this UI modification in
onPostExecute(), but you can also wrap the parts which alter the UI in arunOnUiThread.