Search code examples
androidandroid-fragmentsinterfaceclasscastexception

Interface cannot be cast to Activity


I have created a separate class for the background process. In that, I have two classes and an interface. I am getting string data from the first class and then based on that data I am getting a list of data in another class. The whole process is working fine but now I want to send that list to my fragment but getting java.lang.ClassCastException

below is my code:-

public class PlacesTaskNew extends AsyncTask<String, Void, String> {

    Context context;

    public PlacesTaskNew(Context context) {
        this.context = context;
    }

    @Override
    protected String doInBackground(String... place) {

        String data = "";
        -- --
        return data;
    }

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        ParserTask parserTask = new ParserTask(context);
        parserTask.execute(result);
    }

    private String downloadUrl(String strUrl) throws IOException {

        String data = "";
        -- --
        return data;
    }

    public interface PlaceTaskInterface {
        void onGetPlaces(List<HashMap<String, String>> result, String[] from, int[] to);
    }

    private class ParserTask extends AsyncTask<String, Integer, List<HashMap<String, String>>> {

        JSONObject jObject;
        Context mContext;
        PlaceTaskInterface placeTaskInterface;

        public ParserTask(Context mContext) {
            this.mContext = mContext;
            this.placeTaskInterface = (PlaceTaskInterface) mContext;
        }

        @Override
        protected List<HashMap<String, String>> doInBackground(String... jsonData) {

            List<HashMap<String, String>> places = null;
            --  --
            return places;
        }

        @Override
        protected void onPostExecute(List<HashMap<String, String>> result) {

            String[] from = new String[]{"description"};
            int[] to = new int[]{android.R.id.text1};
            placeTaskInterface.onGetPlaces(result, from, to);
        }
    }

}

And here is my Fragment class :-

public class DashboardFragment extends Fragment implements PlaceTaskInterface {

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

        Locality.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

                //here is the error
                placesTaskNew = new PlacesTaskNew(getActivity());
                placesTaskNew.execute(charSequence.toString());
            }

            @Override
            public void afterTextChanged(Editable editable) {
            }
        });

    }

    @Override
    public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
        PlacesTaskNew placesTaskNew = new PlacesTaskNew(getActivity());
        placesTaskNew.execute(charSequence.toString());
    }

    @Override
    public void onGetPlaces(List<HashMap<String, String>> result, String[] from, int[] to) {
        SimpleAdapter adapter = new SimpleAdapter(getActivity(), result, android.R.layout.simple_list_item_1, from, to);
        Locality.setAdapter(adapter);
    }
}

Below is the logcat error :-

FATAL EXCEPTION: main
Process: com.theweedconnect.me, PID: 14807
java.lang.ClassCastException: MainActivity cannot be cast to PlaceTaskInterface

Please help, Thanx :)


Solution

  • Thanx to @ρяσѕρєя K, here is what I suggest

    Add PlaceTaskInterface placeTaskInterface in both constructors

    PlacesTaskNew construcor

    Context context;
    PlaceTaskInterface placeTaskInterface;
    
    public PlacesTaskNew(Context context, PlaceTaskInterface placeTaskInterface) {
        this.context = context;
        this.placeTaskInterface = placeTaskInterface;
    }
    

    write these lines in onPostExecute method

    @Override
    protected void onPostExecute(String result) {
        super.onPostExecute(result);
        ParserTask parserTask = new ParserTask(context, this.placeTaskInterface);
        parserTask.execute(result);
    }
    

    ParserTask Constructor

    JSONObject jObject;
    Context mContext;
    PlaceTaskInterface placeTaskInterface;
    
    public ParserTask(Context mContext, PlaceTaskInterface placeTaskInterface) {
        this.mContext = mContext;
        this.placeTaskInterface = placeTaskInterface;
    }
    

    And finally in your Fragment, write as belows

    Locality.addTextChangedListener(new TextWatcher() {
    
        @Override
        public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
    
            placesTaskNew = new PlacesTaskNew(getActivity(), new PlacesTaskNew.PlaceTaskInterface() {
                @Override
                public void onGetPlaces(List<HashMap<String, String>> result, String[] from, int[] to) {
                    SimpleAdapter adapter = new SimpleAdapter(getActivity(), result, android.R.layout.simple_list_item_1, from, to);
                    Locality.setAdapter(adapter);
                }
            });
            placesTaskNew.execute(charSequence.toString());
        }
    
        @Override
        public void afterTextChanged(Editable editable) {
        }
    });
    

    Regards!