我试图从扩展BaseAdapter的适配器类中调用一个webservice。我想在这个适配器类中实现接口类。但是当我运行应用程序时,它会抛出ClassCastException。:-
com.mahi.interfaces.AsyncInterface java.lang.ClassCastException:不能将com.mahi.MainActivity转换为com.mahi.MainActivity
这是我的密码:-
public class NewLibsAdapter extends PagerAdapter implements AsyncInterface {
public NewLibsAdapter(Context context, Fragment fragment, int positionNo) {
this.context = context;
inflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.positionNo = positionNo;
userId = AppMethod.getIntegerPreference(context, AppConstant.ID);
SoapObject request = new SoapObject(AppConstant.NAMESPACE, AppConstant.METHOD_NAME_GET_USER_LIBRARY);
request.addProperty("uid", userId);
request.addProperty("reccount", recCount);
DotNetWS logInWS = new DotNetWS(context, request, AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);
logInWS.execute();
}
// This is my interface override method
@Override
public void onWSResponse(String json, String WSType) {
Log.e(TAG, json);
}
}这是我的DotNetWS课程:-
public class DotNetWS extends AsyncTask<String, String, String> {
Context context;
SoapObject request;
AsyncInterface asyncInterface;
String action;
String wsType;
ProgressDialog pDialog;
public DotNetWS(Context context, SoapObject request, String action, String wsType) {
this.context = context;
this.request = request;
this.action = action;
this.wsType = wsType;
this.asyncInterface = (AsyncInterface) context;
}
public DotNetWS(Context context, Fragment fragment, SoapObject request, String action, String wsType) {
this.context = context;
this.request = request;
this.action = action;
this.wsType = wsType;
this.asyncInterface = (AsyncInterface) fragment;
}
@Override
protected void onPreExecute() {
super.onPreExecute();
pDialog = new ProgressDialog(context);
pDialog.setTitle("Connecting...");
pDialog.setMessage("Please Wait...");
pDialog.setCancelable(false);
pDialog.show();
}
@Override
protected String doInBackground(String... params) {
// Coding
}
@Override
protected void onPostExecute(String s) {
super.onPostExecute(s);
pDialog.dismiss();
asyncInterface.onWSResponse(s, wsType);
}
}对这种错误有什么解决办法吗?我可以在适配器类中实现接口类吗?请帮帮忙。谢谢。
发布于 2016-06-03 08:04:26
试试这个,在适配器内部:
DotNetWS logInWS = new DotNetWS(this, request,
AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);
logInWS.execute();您正在发送类似于参数的上下文,但是您的上下文是MainActivity,您必须发送您的适配器类(这个,或NewLibsAdapter.this)。
祝好运!
发布于 2016-06-03 08:05:26
您有两个DotNetWS构造函数,并且正在调用DotNetWS构造函数
DotNetWS logInWS = new DotNetWS(context, request, AppConstant.METHOD_NAME_GET_USER_LIBRARY, AppConstant.GetUserLibraryWS);它将上下文转换为AsyncInterface。真的吗?
this.asyncInterface = (AsyncInterface) context;根据错误文本,此上下文不实现AsyncInterface。
https://stackoverflow.com/questions/37608541
复制相似问题