【问题标题】:Interface cannot be cast to Activity接口不能转换为 Activity
【发布时间】:2017-01-05 06:09:30
【问题描述】:

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

}

这是我的片段类:-

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

请帮忙,谢谢 :)

【问题讨论】:

标签: android android-fragments interface classcastexception


【解决方案1】:

PlaceTaskInterface 是在 Fragment (DashboardFragment) 中实现的,而不是由 getActivity() 方法返回的 Activity

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

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

【讨论】:

  • 能否提供构造函数代码,类中有两个构造函数
  • @Sid:使用public ParserTask(Context mContext,PlaceTaskInterface placeTaskInterface) { this.mContext = mContext; this.placeTaskInterface = placeTaskInterface; }
  • 但是我在同一个班级里打电话给ParserTask parserTask = new ParserTask(context, this);,那么我应该通过什么来代替PlaceTaskInterface placeTaskInterface
  • @Sid: 创建对象时什么都没有,只是通过this
  • 这是说ParserTask(Context, PlaceTaskInterface)不能应用于ParserTask(Context, ParserTaskNew) :(
【解决方案2】:

感谢@ρяσѕρєя 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) {
    }
});

问候!

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-07-28
    • 1970-01-01
    • 1970-01-01
    • 2017-06-22
    • 2011-05-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多