Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 :)

like image 223
Sid Avatar asked Mar 03 '26 10:03

Sid


1 Answers

PlaceTaskInterface is implemented in Fragment (DashboardFragment) instead of Activity which is return by getActivity() method.

Pass this as second parameter to PlacesTaskNew class constructor to get interface instance :

PlacesTaskNew placesTaskNew = new PlacesTaskNew(getActivity(),this);
like image 137
ρяσѕρєя K Avatar answered Mar 05 '26 00:03

ρяσѕρєя K



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!