android数据库中的ClassCastException

时间:2014-04-07 07:17:35

标签: java android sqlite android-asynctask android-sqlite

我有一个MainActivityonCreate

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

任何人都可以帮我找到问题,为什么它会抛出异常。

由于

1 个答案:

答案 0 :(得分:3)

这看起来可能是问题所在:

delegate = (AsynResponse) context;

您将context变量转换为AsynResponsecontext变量来自此处:

new SQLLiteDbHelper(MainActivity.this, ...)

所以它不是 AsynResponse。它甚至不清楚AsynResponse是什么,但MainActivity可能与它不兼容。

您应该弄清楚真正想要该值的内容,并将其单独传递给Context