我有一个MainActivity
,onCreate
是
//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();
}
且sqlitehelper onCreate
和constructor
是
//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());
}
}
我的AsynNetworkOperation
课程是
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
是
public interface AsynResponse {
/**
* This will called when AysncTask finished its execution or when onPostExecute called
*
* @param response
*/
public void responseResult(HttpResponse response);
}
我的问题是,每当sqlitehelper尝试在其AsynNetworkOperation
中执行新的onCreate
时,它就会抛出异常
java.lang.ClassCastException:android.app.MainActivity
任何人都可以帮我找到问题,为什么它会抛出异常。
由于
答案 0 :(得分:3)
这看起来可能是问题所在:
delegate = (AsynResponse) context;
您将context
变量转换为AsynResponse
,context
变量来自此处:
new SQLLiteDbHelper(MainActivity.this, ...)
所以它不是 AsynResponse
。它甚至不清楚AsynResponse
是什么,但MainActivity
可能与它不兼容。
您应该弄清楚真正想要该值的内容,并将其单独传递给Context
。