Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android: async task An error occured while executing doInBackground()

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.

like image 785
Gsurha Avatar asked Feb 24 '26 04:02

Gsurha


1 Answers

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.

like image 71
zkminusck Avatar answered Feb 25 '26 18:02

zkminusck