Android错误onTextChanged更新sqlite数据库

时间:2013-10-23 23:59:52

标签: android sqlite android-edittext

我正在构建一个在ListFragment中显示项目的应用程序。现在每个项目都显示标题和创建日期。还有另外两个片段。一个创建项目并具有EditText字段,我可以编辑标题。另一个只是显示单个项目的内容。

我遇到的问题是,每次我在EditText字段中输入一个字符,应用程序就会关闭。错误消息表明错误发生在TextChangedListener中的onTextChanged上。因为当我将所有内容存储为JSON文件时,我将此功能工作,所以必须发生错误,因为我更新数据库和更新模型层的方式。

此文件执行所有数据库操作并创建自定义Cursor。

public class SnapDatabaseHelper extends SQLiteOpenHelper {
private static final String TAG = "FeedFragment";

private static final String DB_NAME = "snap.sqlite";
private static final int VERSION = 1;

private static final String TABLE_SNAP = "snap";

private static final String COLUMN_SNAP_ID = "_id";
private static final String COLUMN_SNAP_DATE = "snap_date";
private static final String COLUMN_SNAP_UUID = "snap_uuid";
private static final String COLUMN_SNAP_TITLE = "snap_title";

public SnapDatabaseHelper(Context context){
    super(context, DB_NAME, null, VERSION);
}

@Override
public void onCreate(SQLiteDatabase db) {
    // Create SNAP table
    db.execSQL("create table snap(" + 
    "_id integer primary key autoincrement, " +
    //"snap_uuid text, " +
    "snap_date integer, " +
    "snap_title text) ");
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    // 
}

public long insertSnap(Snap snap){
    ContentValues cv = new ContentValues();
    //cv.put(COLUMN_SNAP_UUID, snap.getUniqueId().toString());
    cv.put(COLUMN_SNAP_DATE, snap.getDate().getTime());
    cv.put(COLUMN_SNAP_TITLE, "");
    return getWritableDatabase().insert(TABLE_SNAP, null, cv);

}
public boolean updateTitle(long snapId, String text)
{
 ContentValues cv = new ContentValues();
 cv.put(COLUMN_SNAP_ID, snapId);          
 cv.put(COLUMN_SNAP_TITLE, text);

 int i= getWritableDatabase().update(TABLE_SNAP, cv, COLUMN_SNAP_ID+ "=" + snapId, null);
 return i>0;
}
 public SnapCursor querySnap(long id) {
        Cursor wrapped = getReadableDatabase().query(TABLE_SNAP, 
                null, // all columns 
                COLUMN_SNAP_ID + " = ?", // look for a run ID
                new String[]{ String.valueOf(id) }, // with this value
                null, // group by
                null, // order by
                null, // having
                "1"); // limit 1 row
        return new SnapCursor(wrapped);
 }
 public SnapCursor querySnaps() {
        // equivalent to "select * from run order by start_date asc"
        Cursor wrapped = getReadableDatabase().query(TABLE_SNAP,
                null, null, null, null, null, COLUMN_SNAP_DATE + " asc");
        return new SnapCursor(wrapped);
    }

public static class SnapCursor extends CursorWrapper{
    public SnapCursor(Cursor c){
        super(c);
    }
    public Snap getSnap() {
        if (isBeforeFirst() || isAfterLast())
            return null;

        Snap s = new Snap();
        s.setId(getLong(getColumnIndex(COLUMN_SNAP_ID)));
        //s.setUniqueId(UUID(getString(getColumnIndex(COLUMN_SNAP_UUID))));
        s.setDate(new Date(getLong(getColumnIndex(COLUMN_SNAP_DATE))));
        s.setTitle(getString(getColumnIndex(COLUMN_SNAP_TITLE)));
        return s;
    }
}

}

此文件将片段链接到DatabaseHelper。

public class SnapLab {

private static SnapLab sSnapLab;
private Context mAppContext;
private SnapDatabaseHelper mHelper;

// private constructor
private SnapLab(Context appContext){
    mAppContext = appContext;
    mHelper = new SnapDatabaseHelper(mAppContext);
}
public static SnapLab get(Context c){
    if(sSnapLab == null){
        sSnapLab = new SnapLab(c.getApplicationContext());
    }
    return sSnapLab;
}
public Snap insertSnap() {
    Snap s = new Snap();
    s.setId(mHelper.insertSnap(s));

    return s;
}
public boolean updateTitle(long snapId, String text){


    return  mHelper.updateTitle(snapId, text);

}

public SnapCursor querySnaps() {
    return mHelper.querySnaps();
}

public Snap getSnap(long id) {
    Snap s = null;
    SnapCursor cursor = mHelper.querySnap(id);
    cursor.moveToFirst();
    // if we got a row, get a run
    if (!cursor.isAfterLast())
        s = cursor.getSnap();
    cursor.close();
    return s;
}

}

这是带有EditText字段的片段

public class EditPageFragment extends Fragment {
private static final String TAG = "EditPageFragment";
public static final String EXTRA_SNAP_ID = "SNAP_ID";

private SnapLab mSnapLab;
private Snap mSnap;
private SnapDatabaseHelper mHelper;
private EditText mSnapText;
private Button mUploadButton;
private TextView mDateText;
private Long snapId;

public static EditPageFragment newInstance(Long snapId){
    Bundle args = new Bundle();
    args.putLong(EXTRA_SNAP_ID, snapId);
    EditPageFragment fragment = new EditPageFragment();
    fragment.setArguments(args);
    return fragment;
}

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setRetainInstance(true);
    mSnapLab = SnapLab.get(getActivity());

    Bundle args = getArguments();
    if (args != null){
        long snapId = args.getLong(EXTRA_SNAP_ID, -1);
        if (snapId != -1){
            mSnap = mSnapLab.getSnap(snapId);
        }
    }
    mSnap = new Snap();
    mSnap = mSnapLab.insertSnap();
}   

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent, Bundle   savedInstanceState){
    View v = inflater.inflate(R.layout.edit_fragment, parent, false);


    mDateText = (TextView)v.findViewById(R.id.edit_dateText);
    mDateText.setText(mSnap.getDate().toString());

    mSnapText = (EditText)v.findViewById(R.id.edit_snapText);
    mSnapText.addTextChangedListener(new TextWatcher(){
        @Override
        public void afterTextChanged(Editable s) {
            //leave blank for now
        }
        @Override
        public void beforeTextChanged(CharSequence c, int start, int count,
                int after) {
            //leave blank for now
        }
        @Override
        public void onTextChanged(CharSequence c, int start, int before,
                int count) {
            mSnap.setTitle(c.toString());
            mSnapLab.updateTitle(snapId, c.toString());

            Log.i(TAG, "text saved");
        }
    });

    return v;
}
}

代码的导入位是updateTitle()函数。我能做错什么?您对如何更好地更新数据库有一个建议吗?除了更新标题外,一切都很有效。我很感激你的帮助。

1 个答案:

答案 0 :(得分:0)

看起来没有分配snapId

private Long snapId; //field

以后几行

long snapId = args.getLong(EXTRA_SNAP_ID, -1); //local variable

以后几行

mSnapLab.updateTitle(snapId, c.toString()); //field

请在下次添加stacktrace。