-2

I have an AsyncTask GoogleBookAsyncTask that I want to run in a Fragment. I am following this other SO post. It requires my Fragment class BookListingActivityFragment to implement an interface AsyncResponse and declare its abstract method onProcessFinish().

The onProcessFinish() in my Fragment is declare as an anonymous class life following.

public GoogleBookAsyncTask mGoogleBookAsyncTask = new GoogleBookAsyncTask(new AsyncResponse() {
    @Override
    public void processFinish(ArrayList<Book> bookArrayList) {
        mBookArrayAdapter.clear();
        mBookArrayAdapter = new BookAdapter(getContext(), bookArrayList);
        mBookArrayAdapter.notifyDataSetChanged();
    }
});

I get an error must either be declared abstract or implement abstract method. This error goes away if I declare processFinish() as a regular non-inner class. How can I resolve this error? I need to execute mGoogleBookAsyncTask.execute() to get data from my AsyncTask class.

The full source code my app can be found here.

Community
  • 1
  • 1
Sudhir Singh Khanger
  • 1,598
  • 2
  • 17
  • 34
  • 1
    "This error goes away if I declare onProcessFinish() as a regular non-inner class" -- `onProcessFinish()` is a method. It is not a class. "The full source code my app can be found here." -- there is no `AsyncTask` in that repository. Nor is there a `GoogleBookAsyncTask` or an `AsyncResponse`. Please provide a [mcve], including the source code for `GoogleBookAsyncTask` and `AsyncResponse`. – CommonsWare Aug 20 '16 at 14:03
  • Hi @CommonsWare I am sorry about that. I have fixed the project link and the onProcessFinish() typo. Please take a look. – Sudhir Singh Khanger Aug 20 '16 at 18:24
  • 1
    "I get an error must either be declared abstract or implement abstract method" -- what abstract method is it saying that you should implement? Or, if your IDE supports a quick-fix to add the method, what method does it add? BTW, this code should crash (`mBookArrayAdapter` will be `null`), and there is no point in calling `notifyDataSetChanged()` on a adapter that is not attached to an `AdapterView`. – CommonsWare Aug 20 '16 at 18:41
  • `BookListingActivityFragment` implements interface `AsyncResponse` which has one method `processFinish(ArrayList<>)`. As a result I have to implement `processFinish(ArrayList<>)` in the BookListingActivityFragment class. I have implemented `processFinish` as an annonymous class of an instance of GoogleBookAsyncTask. See the error http://imgur.com/a/yMjMA. If I use implement `AsyncResponse` I have to declare processFinish but if I don't implement `AsyncResponse` interface I can still run everything and it works. if `mBookArrayAdapter` is null I set a TextView which shows `no data found` text. – Sudhir Singh Khanger Aug 21 '16 at 01:49

2 Answers2

2

If I implement AsyncResponse, I have to declare processFinish

Yes, that is exactly what that error tells you.

but if I don't implement AsyncResponse interface I can still run everything and it works.

Then why care about the error at all?


If you want to fix the problem, then do implement the interface on the class, and instead do this.

public class BookListingActivityFragment extends Fragment 
        implements AsyncResponse {

    private GoogleBookAsyncTask mGoogleBookAsyncTask;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
            // .......

            // Pass "delegate" interface as 'this'
            mGoogleBookAsyncTask = new GoogleBookAsyncTask(this);
            mmGoogleBookAsyncTask.execute();
        }

    // This is the interface implementation that is called from the AsyncTask
    @Override
    public void processFinish(ArrayList<Book> bookArrayList) {
        mBookArrayAdapter.clear();
        mBookArrayAdapter = new BookAdapter(getContext(), bookArrayList);
        mBookArrayAdapter.notifyDataSetChanged();
    }

}
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
  • Thanks for commenting. What does `this` reference to in `new GoogleBookAsyncTask(this);`? – Sudhir Singh Khanger Aug 21 '16 at 03:25
  • Can you please explain why it wasn't working the way it was explained in the SO post? https://stackoverflow.com/questions/12575068/how-to-get-the-result-of-onpostexecute-to-main-activity-because-asynctask-is-a/12575319#12575319 – Sudhir Singh Khanger Aug 21 '16 at 04:15
  • The "first" solution there looks correct. I've edited the second solution so it compiles. Thank you for pointing that out. – OneCricketeer Aug 21 '16 at 05:16
0

If the error is what I think it is, it should say this almost exactly: anonymous class <your class> is not abstract, and does not implement method <some method> from <some interface>. If this is what you are seeing, then that means you are not implementing another abstract method in that interface.

Ian S.
  • 1,831
  • 9
  • 17