我需要帮助。
我有一个带有自定义适配器的listview,当在这个listview中有一个复选框时,in onclick方法应该更新数据库中的一个表。
复选框的onclick方法是在适配器类中实现的,当我单击某个复选框时,我会在创建listview时调用另一个类的方法。
在我的适配器类中
γ
chkQuitado.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
CheckBox cb = (CheckBox) v;
ViewMovimentos vm = new ViewMovimentos();
int movId = Integer.parseInt(entry.getMovId());
if(cb.isChecked()){
vm.confirmaQuitacao(context, movId); //Implementada na classe ViewMovimentos
}else{
vm.desfazQuitacao(context, movId); //Implementada na classe ViewMovimentos
}
}
});
γ
在movimentos.class中,我实现了方法confirmaquitaco()和desfazquitaco()
γ
public void confirmaQuitacao(Context context, final int movId){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Confirma a quitação deste movimento?");
builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitaMovimento(movId, MOV_FECHADO);
}
});
builder.setNeutralButton("Não", null);
builder.setTitle("Aviso");
builder.show();
}
public void desfazQuitacao(Context context, final int movId){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Deseja marcar esse movimento como não quitado?");
builder.setPositiveButton("Sim", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
quitaMovimento(movId, MOV_ABERTO);
}
});
builder.setNeutralButton("Não", null);
builder.setTitle("Aviso");
builder.show();
}
γ
当这两个方法调用quitamovimento方法时。
γ
public void quitaMovimento(int movId, int quitar){
SQLiteDatabase banco = openOrCreateDatabase(nmBanco, MODE_WORLD_WRITEABLE, null);
ContentValues camposPar = new ContentValues();
camposPar.put("movQuitado", quitar);
banco.update("movimentos", camposPar, "movId = "+movId, null);
localizaRegistros();
}
γ
所以,当我调用openorcreatedatabase()时,我的应用程序中会发生强制关闭,而logcat会显示下面的错误。
γ
02-14 22:47:33.645: E/AndroidRuntime(367): Uncaught handler: thread main exiting due to uncaught exception
02-14 22:47:33.749: E/AndroidRuntime(367): java.lang.NullPointerException
02-14 22:47:33.749: E/AndroidRuntime(367): at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:193)
02-14 22:47:33.749: E/AndroidRuntime(367): at com.simpatico.sgf.ViewMovimentos.quitaMovimento(ViewMovimentos.java:433)
02-14 22:47:33.749: E/AndroidRuntime(367): at com.simpatico.sgf.ViewMovimentos$7.onClick(ViewMovimentos.java:450)
02-14 22:47:33.749: E/AndroidRuntime(367): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:158)
02-14 22:47:33.749: E/AndroidRuntime(367): at android.os.Handler.dispatchMessage(Handler.java:99)
02-14 22:47:33.749: E/AndroidRuntime(367): at android.os.Looper.loop(Looper.java:123)
02-14 22:47:33.749: E/AndroidRuntime(367): at android.app.ActivityThread.main(ActivityThread.java:4363)
02-14 22:47:33.749: E/AndroidRuntime(367): at java.lang.reflect.Method.invokeNative(Native Method)
02-14 22:47:33.749: E/AndroidRuntime(367): at java.lang.reflect.Method.invoke(Method.java:521)
02-14 22:47:33.749: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:860)
02-14 22:47:33.749: E/AndroidRuntime(367): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618)
02-14 22:47:33.749: E/AndroidRuntime(367): at dalvik.system.NativeStart.main(Native Method)
γ
有人能帮我解决这个问题吗?
谢谢
最佳答案
openOrCreateDatabase
的一些参数被记录为可选的,但这并不意味着您可以为这些参数传递null
;这意味着您应该在根本不传递这些参数的地方使用重载。
以下是一种适合您的语法:
SQLiteDatabase banco = openDatabase(nmBanco, MODE_WORLD_WRITEABLE, SQLiteDatabase.CREATE_IF_NECESSARY)