Home>
Asynctask is executed only in the onCreate method

OnCreate method moves asynctask, but does not move when spinner or button is pressed.

I don't know how to solve this problem.

Can asynctask be called only by the onCreate method? Or is the call wrong?

Error message Message when clicking
spinner
W/System.err: java.lang.IllegalStateException: Observer [email protected] was not registered.
        at android.database.Observable.unregisterObserver (Observable.java:69)
08-27 11: 20: 30.856 11772-11814/com.example.sample.test W/System.err: at android.widget.BaseAdapter.unregisterDataSetObserver (BaseAdapter.java:42)
        at android.widget.ListView.setAdapter (ListView.java:472)
        at com.example.sample.test.MainActivity $AsyncTaskA.doInBackground (MainActivity.java:307)
        at com.example.sample.test.MainActivity $AsyncTaskA.doInBackground (MainActivity.java:256)
        at android.os.AsyncTask $2.call (AsyncTask.java:305)
        at java.util.concurrent.FutureTask.run (FutureTask.java:237)
        at android.os.AsyncTask $SerialExecutor $1.run (AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
        at java.util.concurrent.ThreadPoolExecutor $Worker.run (ThreadPoolExecutor.java:607)
        at java.lang.Thread.run (Thread.java:761)
Message when button is clicked
W/System.err: java.lang.IllegalStateException: Observer [email protected] was not registered.
08-27 11: 21: 53.449 11772-11825/com.example.sample.test W/System.err: at android.database.Observable.unregisterObserver (Observable.java:69)
        at android.widget.BaseAdapter.unregisterDataSetObserver (BaseAdapter.java:42)
        at android.widget.ListView.setAdapter (ListView.java:472)
08-27 11: 21: 53.450 11772-11825/com.example.sample.test W/System.err: at
com.example.sample.test.MainActivity $AsyncTaskA.doInBackground (MainActivity.java:307)
        at com.example.sample.test.MainActivity $AsyncTaskA.doInBackground (MainActivity.java:256)
        at android.os.AsyncTask $2.call (AsyncTask.java:305)
        at java.util.concurrent.FutureTask.run (FutureTask.java:237)
        at android.os.AsyncTask $SerialExecutor $1.run (AsyncTask.java:243)
        at java.util.concurrent.ThreadPoolExecutor.runWorker (ThreadPoolExecutor.java:1133)
08-27 11: 21: 53.451 11772-11825/com.example.sample.test W/System.err: at java.util.concurrent.ThreadPoolExecutor $Worker.run (ThreadPoolExecutor.java:607)
        at java.lang.Thread.run (Thread.java:761)
Applicable source code
/* What happens when you click the spinner * /
   private class selectSpinner implements AdapterView.OnItemSelectedListener {
            public void onItemSelected (AdapterView<?>parent, View arg1, int arg2, long arg3) {
                // Get the index of the selected item
                int idx = spinner.getSelectedItemPosition ();
                // I want to change the processing of AsyncTask by Index
                if (idx == 0) {
                    new AsyncTaskA (). execute ("args as doInBackground");
                } else {
                    new AsyncTaskB (). execute ("args as doInBackground");
                }
            }
            public void onNothingSelected (AdapterView<?>parent) {
            }
    }
/ * Button to communicate again when communication fails * /
private class retryListener implements View.OnClickListener {
        @Override
        public void onClick (View view) {
            // Get the index of the selected item
            int idx = spinner.getSelectedItemPosition ();
            // I want to change the processing of AsyncTask by Index
            if (idx == 0) {
                new AsyncTaskA (). execute ("args as doInBackground");
            } else {
                new AsyncTaskB (). execute ("args as doInBackground");
            }
        }
    }
  • Answer # 1

    If you look at the error log, the listAd's setAdapter method is called in thedoInBackgroundmethod of AsyncTask. On Android, do not perform UI-related operations from threads other than the main thread.

    If you want to update the ListView display based on the result of asynchronous processing, try to modify it in theonPostExecutemethod of AsyncTask.