将记录添加到数据库,程序出错
日志
04-02 09:22:08.601: W/dalvikvm(4332): threadid=1: thread exiting with uncaught exception (group=0x40018578)
04-02 09:22:10.023: E/AndroidRuntime(4332): FATAL EXCEPTION: main
04-02 09:22:10.023: E/AndroidRuntime(4332): java.lang.NullPointerException
04-02 09:22:10.023: E/AndroidRuntime(4332): at com.example.ok1.MySqlCursorAdapter.onClick(MySqlCursorAdapter.java:87)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.view.View.performClick(View.java:2485)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.widget.CompoundButton.performClick(CompoundButton.java:99)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.view.View$PerformClick.run(View.java:9080)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.os.Handler.handleCallback(Handler.java:587)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.os.Handler.dispatchMessage(Handler.java:92)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.os.Looper.loop(Looper.java:130)
04-02 09:22:10.023: E/AndroidRuntime(4332): at android.app.ActivityThread.main(ActivityThread.java:3687)
04-02 09:22:10.023: E/AndroidRuntime(4332): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 09:22:10.023: E/AndroidRuntime(4332): at java.lang.reflect.Method.invoke(Method.java:507)
04-02 09:22:10.023: E/AndroidRuntime(4332): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:867)
04-02 09:22:10.023: E/AndroidRuntime(4332): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:625)
04-02 09:22:10.023: E/AndroidRuntime(4332): at dalvik.system.NativeStart.main(Native Method)
MySqlCursorAdapter
package com.example.ok1;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.SimpleCursorAdapter;
import android.widget.TextView;
public class MySqlCursorAdapter extends SimpleCursorAdapter implements
OnClickListener {
final String Tag = "States";
private Context context;
private DBHelper dbHelper;
private Cursor currentCursor;
public MySqlCursorAdapter(Context context, int layout, Cursor c,
String[] from, int[] to, DBHelper dbHelper) {
super(context, layout, c, from, to);
Log.d(Tag, "трассировка1");
this.currentCursor = c;
this.context = context;
this.dbHelper = dbHelper;
Log.d(Tag, "MySqlCursorAdapter()");
Integer b = c.getCount();
Log.d(Tag, "b=" + b);
}
public View getView(int pos, View inView, ViewGroup parent) {
Log.d(Tag, "getView() + posss=" + pos);
View v = inView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.my_list_item, null);
}
this.currentCursor.moveToPosition(pos);
CheckBox cBox = (CheckBox) v.findViewById(R.id.bcheck);
cBox.setTag(Integer.parseInt(this.currentCursor
.getString(this.currentCursor
.getColumnIndex(DBHelper.COLUMN_ID))));
Log.d(Tag, "tag=" + cBox.getTag().toString());
if (this.currentCursor.getString(this.currentCursor
.getColumnIndex(DBHelper.COLUMN_STATUS)) != null
&& Integer.parseInt(this.currentCursor
.getString(this.currentCursor
.getColumnIndex(DBHelper.COLUMN_STATUS))) != 0) {
cBox.setChecked(true);
} else {
cBox.setChecked(false);
}
cBox.setOnClickListener(this);
TextView txtTitle = (TextView) v.findViewById(R.id.txtTitle);
txtTitle.setText(this.currentCursor.getString(this.currentCursor
.getColumnIndex(DBHelper.COLUMN_NAME)));
return (v);
}
public void ClearSelections() {
Log.d(Tag, "ClearSelections()");
this.dbHelper.clearSelections();
this.currentCursor.requery();
}
@Override
public void onClick(View v) {
Log.d(Tag, "onClick");
CheckBox cBox = (CheckBox) v;
Integer _id = (Integer) cBox.getTag();
Log.d(Tag, "Integer _id=" + _id.toString());
ContentValues values = new ContentValues();
values.put(" status", cBox.isChecked() ? 1 : 0);
try {
this.dbHelper.dbSqlite.update("mytable", values, "_id = ?",
new String[] { Integer.toString(_id) });
} catch (SQLException sqle) {
Log.d(Tag, "неудача");
throw sqle;
}
}
}
错误出现在该行
"this.dbHelper.dbSqlite.update("mytable", values, "_id = ?", new String[] { Integer.toString(_id) });"`