【发布时间】: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
请帮忙,谢谢 :)
【问题讨论】:
-
我可以给我你的
SimpleAdapter代码吗?
标签: android android-fragments interface classcastexception