我有一个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 onCreateconstructor

//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

谁能帮我找到问题的原因,为什么它会引发异常。

谢谢

最佳答案

看来这可能是问题所在:

delegate = (AsynResponse) context;


您正在将context变量强制转换为AsynResponse,而该context变量来自此处:

new SQLLiteDbHelper(MainActivity.this, ...)


因此它不是AsynResponse。甚至还不清楚AsynResponse是什么,但是MainActivity可能与它不兼容。

您应该算出您真正想要的值是什么,可以将其分别传递给Context

关于java - Android数据库中的ClassCastException,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/22905849/

10-09 18:21