gpt4 book ai didi

java - Android 数据库中的 ClassCastException

转载 作者:行者123 更新时间:2023-12-01 23:05:08 24 4
gpt4 key购买 nike

我有一个 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 
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 尝试在其 onCreate 中执行新的 AsynNetworkOperation 时,它都会抛出异常

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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com