接口无法强制转换为Activity

Sid*_*Sid 1 android interface classcastexception android-fragments

我为后台进程创建了一个单独的类.在那,我有两个类和一个接口.我从第一个类中获取字符串数据,然后根据该数据获取另一个类中的数据列表.整个过程工作正常,但现在我想将该列表发送到我的片段但是得到了java.lang.ClassCastException

以下是我的代码: -

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);
        }
    }

}
Run Code Online (Sandbox Code Playgroud)

这是我的Fragment类: -

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);
    }
}
Run Code Online (Sandbox Code Playgroud)

以下是logcat错误: -

FATAL EXCEPTION: main
Process: com.theweedconnect.me, PID: 14807
java.lang.ClassCastException: MainActivity cannot be cast to PlaceTaskInterface
Run Code Online (Sandbox Code Playgroud)

请帮忙,Thanx :)

ρяσ*_*я K 7

PlaceTaskInterfaceFragment(DashboardFragment)中实现,而不是Activity通过getActivity()方法返回.

this作为第二个参数传递给PlacesTaskNew类构造函数以获取接口实例:

PlacesTaskNew placesTaskNew = new PlacesTaskNew(getActivity(),this);
Run Code Online (Sandbox Code Playgroud)