接口无法转换为 Activity

Interface cannot be cast to Activity

我已经为后台进程创建了一个单独的 class。在那里,我有两个 classes 和一个接口。我从第一个 class 中获取字符串数据,然后根据该数据在另一个 class 中获取数据列表。整个过程工作正常但现在我想将该列表发送到我的片段但得到 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);
        }
    }

}

这是我的片段 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);
    }
}

下面是 logcat 错误:-

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

请帮忙,谢谢 :)

PlaceTaskInterface 是在 Fragment (DashboardFragment) 中实现的,而不是 Activity 是 return 通过 getActivity() 方法实现的。

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

PlacesTaskNew placesTaskNew = new PlacesTaskNew(getActivity(),this);

感谢@ρяσѕρєя K,这是我的建议

在两个构造函数中添加PlaceTaskInterface placeTaskInterface

PlacesTaskNew 构造函数

Context context;
PlaceTaskInterface placeTaskInterface;

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

onPostExecute方法中写下这些行

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

ParserTask 构造函数

JSONObject jObject;
Context mContext;
PlaceTaskInterface placeTaskInterface;

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

最后在你的Fragment中,写成如下

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

此致!