Reputation: 5068
I have a MainActivity
whose onCreate
is
//called when activity first created
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
searchText = (EditText) findViewById(R.id.searchText);
utils = new Utils(MainActivity.this);
db=(new SQLLiteDbHelper(MainActivity.this,utils.getInt(Key.Db_version))).getReadableDatabase();
request = new WebRequest(this);
status = new AsynGetEmployeeStatus();
}
and sqlitehelper onCreate
and constructor
is
//constructor
public SQLLiteDbHelper(Context context,int dbVersion) {
super(context,DATABASE_NAME, null, dbVersion);
this.context = context;
Log.d("tag","db version is "+DATABASE_VERSION);
crypt = new Cryptography();
utils = new Utils(context);
}
//called when activity first created
@Override
public void onCreate(final SQLiteDatabase db) {
String s;
try {
//new LoadData().execute(db);
new AsynNetworkOperation(context, ServiceUri.AccessMethod.GET, "loading").execute(ServiceUri.SERVICE_URI+"s?d=abc"); //this line throw exception
} catch (Throwable t) {
Toast.makeText(context, t.toString(), Toast.LENGTH_LONG).show();
Log.d("tag",t.toString());
}
}
my AsynNetworkOperation
class is
public class AsynNetworkOperation extends AsyncTask<String,Void,Void>{
private Context context = null;
private ProgressDialog dialog = null;
private String title = "";
private WebRequest request = null;
private String accessMethod = "";
private HttpResponse response = null;
AsynResponse delegate = null;
public AsynNetworkOperation(Context context, String method, String dialogTitle)
{
this.context = context;
accessMethod = method;
this.title = dialogTitle;
delegate = (AsynResponse) context;
}
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog = new ProgressDialog(context);
dialog.setMessage(title);
dialog.setCanceledOnTouchOutside(false);
dialog.show();
}
@Override
protected Void doInBackground(String... data) {
// TODO Auto-generated method stub
request = new WebRequest(context);
if(accessMethod.equals(ServiceUri.AccessMethod.GET)){
response = request.makeHttpGetCall(data[0]);
}
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
dialog.dismiss();
delegate.responseResult(response);
dispose();
}
private void dispose()
{
context = null;
dialog = null;
title = "";
request = null;
accessMethod = "";
delegate = null;
}
}
AsynResponse
is
public interface AsynResponse {
/**
* This will called when AysncTask finished its execution or when onPostExecute called
*
* @param response
*/
public void responseResult(HttpResponse response);
}
My problem is , whenever sqlitehelper try to execute new AsynNetworkOperation
in its onCreate
, it throw exception
java.lang.ClassCastException: android.app.MainActivity
can anyone help me finding the problem why it is throwing exception.
Thanks
Upvotes: 0
Views: 107
Reputation: 1501926
Well this looks like it's probably the problem:
delegate = (AsynResponse) context;
You're casting the context
variable to AsynResponse
, and that context
variable comes from here:
new SQLLiteDbHelper(MainActivity.this, ...)
So it isn't an AsynResponse
. It's not even clear what AsynResponse
is, but MainActivity
presumably isn't compatible with it.
You should work out what you really want that value to be, potentially passing it separately to the Context
.
Upvotes: 3