android - 向数据库中添加记录,程序出错

标签 android

向数据库添加记录,程序出错

日志

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)

MySql游标适配器

   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) });"`

最佳答案

请检查您是否反对 DBHelper 类,因为 dbsqlite 是否已初始化

dbHelper.dbSqlite

正如您在问题中提到的那样,引用了这一行。可能是你的 dbSqlite 没有初始化

if(dbHelper.dbSqlite==null)
    // init first then used

关于android - 向数据库中添加记录,程序出错,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15756978/

相关文章:

android - SwipeRefreshLayout 在 onRefresh() 开始之前检测向下滑动事件

java - 运行应用程序时只能看到主屏幕,看不到我的应用程序

android - 调试时禁用 ANR 消息

android - 为什么 Android WebView 在 Horizo​​ntalScrollView 上闪烁?

android - 如何从 ListView onClickAdapter 的数据库中删除一行?

Android Studio/ADB 突然停止初始化/连接/工作

java - 如何解决此错误 abc_list_selector_disabled_holo_light.9.png?

android - MPAndroidChart 单击后更改切片颜色

android - PagerAdapter 的 onclick 方法

android - 如何将首选项动态添加到首选项屏幕并绑定(bind)它们的值?