Reputation: 43
before use Async Tasks my code is work. So i have done some changes in my code and now progress bar is showing up but getting the below errors.
Log cat:
08-07 06:43:15.875: E/AndroidRuntime(1734): FATAL EXCEPTION: AsyncTask #1
08-07 06:43:15.875: E/AndroidRuntime(1734): java.lang.RuntimeException: An error occured while executing doInBackground()
08-07 06:43:15.875: E/AndroidRuntime(1734): at android.os.AsyncTask$3.done(AsyncTask.java:299)
08-07 06:43:15.875: E/AndroidRuntime(1734): at java.util.concurrent.FutureTask$Sync.innerSetException(FutureTask.java:273)
08-07 06:43:15.875: E/AndroidRuntime(1734): at java.util.concurrent.FutureTask.setException(FutureTask.java:124)
08-07 06:43:15.875: E/AndroidRuntime(1734): ... 18 more
mycode:
protected String doInBackground(String... args) {
final EditText eKey = (EditText) findViewById(R.id.edtxt_key);
inputs = eKey.getText().toString();
if (inputs.matches("")) {
} else {
keys = url + inputs;
JSONParser jParser = new JSONParser();
JSONObject json = jParser.getJSONFromUrl(keys);
try {
satu = json.getJSONObject(TAG_1);
String two = satu.getString(TAG_2);
String three = satu.getString(TAG_3);
if (two.matches("") | three.matches("")) {
AlertDialog.Builder builder = new AertDialog.Builder(MainActivity.this);
builder.setMessage("not found!").setCancelable(false).setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
eKey.setText("");
status.setText("Key not found!");
}
});
AlertDialog alert = builder.create();
alert.show();
} else {
Intent x = new Intent(MainActivity.this, result.class);
x.putExtra("bac", two);
x.putExtra("cab", three);
startActivity(x);
eKey.setText("");
keys = "";
inputs = "";
}
} catch (JSONException e) {
e.printStackTrace();
}
}
return null;
}
What is going wrong? Thanks in advance.
Upvotes: 2
Views: 15883
Reputation: 359
AsynTask class provides three main methods :
the two methods onPreExecute(), onPostExecute() are executed in the same thread of the class which contains the UI.
The method doInBackground() is executed on a different thread. As we cannot refer to UI from a process that is running on a different thread, we can :
Bye :)
Upvotes: 1
Reputation: 101
You should not keep any UI related element inside doInBackground()
For example,
If you are downloading any staff from network, at that time you want to update UI , you can use onProgressUpdate()
to update UI and implement publishProgress() @doInBackground().
Otherwise,
AsynTask is running in separate worker thread to execute staff behind UI using doInBackground()
method (We can show loading dialog when Asyntask running in background). Once work is done, we can update UI using onPostExecute()
.
If you need more clarification on AsynTask, sure u can shot your questions...
Happy coding :)
Upvotes: 2
Reputation: 1240
alert.show();
and
eKey.setText("");
cannot be performed in background task. Pass a return value from
doInBackground()
to
onPostExecute()
method and perform UI tasks there.
Upvotes: 8