Sudhir Khanger Sudhir Khanger - 3 months ago 12
Java Question

Declared as an inner class but error "must be declared abstract or implement" persists

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.

Answer

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) {
            // .......

            mGoogleBookAsyncTask = new GoogleBookAsyncTask(this);
            mmGoogleBookAsyncTask.execute();
        }

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

}
Comments