-1

我有一个可扩展的列表适配器,其中包含每个类别下的类别和项目。如果我单击删除一个项目,将弹出一个警告对话框,确认删除。当我按下yes调试器时,总是停dbHelper.deleteItemizedSpendingNullPointerException. 我在里面设置了一个断点,deleteItemizedSpending但它没有进入fn。

数据库已经初始化并打开以在主要活动中写入。我还检查了参数group.category_name, children.row_id,children.item_amount都是有效的。这里有什么问题?

文件 ExpandableCategoryListAdapter.java(仅复制相关代码):

public class ExpandableCategoryListAdapter extends BaseExpandableListAdapter {

private DbHelper dbHelper; 

public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
final Children children = (Children) getChild(groupPosition, childPosition);
// get a hold of layout elements
if (convertView == null) {
convertView = inflater.inflate(R.layout.child_row, null);
}
TextView tag = (TextView) convertView.findViewById(R.id.textCategoryItemTag);
TextView amount = (TextView) convertView.findViewById(R.id.textCategoryItemAmount);
ImageView edit = (ImageView) convertView.findViewById(R.id.editCategoryItem);
ImageView delete = (ImageView) convertView.findViewById(R.id.deleteCategoryItem);
tag.setText(children.tag_name);
amount.setText(Integer.toString(children.item_amount));

// method to handle edit item icon click

// method to handle delete item icon click
delete.setOnClickListener(new OnClickListener() {        
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(activity);
builder.setMessage("Delete?");
builder.setCancelable(true);
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {

Group group = (Group) getGroup(groupPosition);
dbHelper.deleteItemizedSpending(group.category_name, children.row_id, 
children.item_amount);                                                 
// then remove row from dataset and notify that it changed
List<Children> child = groups.get(groupPosition).children;
child.remove(childPosition);
notifyDataSetChanged();
}
});

文件 DbHelper.java(仅复制相关代码):

public class DbHelper {
private Context context;
private SQLiteDatabase db;
private Database dbHelper;

public DbHelper(Context context) {
this.context = context;
dbHelper = new Database(this.context);
}

public void open() throws SQLException {
db = dbHelper.getWritableDatabase();
}

public void deleteItemizedSpending(String table, long id, int amount) {
int travel_id = fetchTravelId(table, id, amount);

try {db.delete(
table, 
DatabaseContract.CategoryTable._ID + "=?" + " AND " +
DatabaseContract.CategoryTable.COLUMN_AMOUNT + "=?",
new String[] {String.valueOf(id), String.valueOf(amount)});}
catch (Exception e)
{
Log.e("DB ERROR 3", e.toString());
e.printStackTrace();
}

堆栈跟踪:

DalvikVM[localhost:8603]
线程 [<1> main] (Suspended (exception NullPointerException))
ExpandableCategoryListAdapter$1$1.onClick(DialogInterface, int) line: 70
AlertController$ButtonHandler.handleMessage(Message) line: 167
AlertController$ButtonHandler(Handler) .dispatchMessage (Message) 行:99
Looper.loop() 行:137 ActivityThread.main(String[]) 行:4950
Method.invokeNative(Object, Object[], Class, Class[], Class, int, boolean) 行:不可用 [本机方法]
Method.invoke(Object, Object...) 行:511
ZygoteInit$MethodAndArgsCaller.run() line: 1004 ZygoteInit.main(String[]) line: 771 NativeStart.main(String[]) line: not available [native method]
Thread [<10> Binder_2] (Running)
Thread [< 9> Binder_1](运行中)线程 [<11> Binder_3](运行中)

4

1 回答 1

0

变量 dbHelper 未初始化...

private DbHelper dbHelper;

此行崩溃

dbHelper.deleteItemizedSpending(group.category_name, children.row_id, children.item_amount);   
于 2013-09-08T00:41:52.087 回答