为什么我不能将第二个EditText字段中的值添加到数据库中?

时间:2014-06-15 10:09:37

标签: java android sqlite

我认为自己是Android开发世界的新手。最近,当我在玩SQLite数据库时,我遇到了一个特殊的问题。我可以在跑步时查看所有内容。但是当我尝试通过单击添加按钮添加内容时,系统就会关闭。这是我的代码。如果你知道,请帮忙。非常感激。

感谢大家的帮助。最初的问题已经解决但我仍然对我的代码感到困惑,因为我仍然无法找到错误的来源。为什么数据库只返回第一个EditText的值而不是第二个EditText字段的值?再次感谢。

提供SQLHelper

package com.example.sgdriverdiary;

import java.text.SimpleDateFormat;
import java.util.Date;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

// A helper class to manage database creation and version management. 
public class MySQLiteHelper extends SQLiteOpenHelper {
    private static final String DB_NAME = "mydb";
    private static final int DB_VERSION = 1;

    public static final String TABLE_NAME = "record";
    public static final String COL_LITRE = "rLitre";
    public static final String COL_DATE = "rDate";
    public static final String COL_MILE = "rMile";
    private static final String STRING_CREATE = "CREATE TABLE " + TABLE_NAME + " (_id INTEGER PRIMARY KEY AUTOINCREMENT, " + COL_DATE+" DATE, "+COL_MILE+" TEXT, "+COL_LITRE+" TEXT);"; 
    public MySQLiteHelper(Context ontext){
        super(ontext, DB_NAME, null, DB_VERSION);
    }
    @Override
    public void onCreate(SQLiteDatabase db){
        db.execSQL(STRING_CREATE);
        ContentValues cv = new ContentValues(3);
        cv.put(COL_LITRE, "6");
        cv.put(COL_MILE, "500");
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cv.put(COL_DATE,  dateFormat.format(new Date()));
        db.insert(TABLE_NAME, null,cv);
    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        // TODO Auto-generated method stub
        db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
        onCreate(db);
    }
}

RES /布局/记录

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.sgdriverdiary.Record$PlaceholderFragment"
    android:background="@drawable/layout_border" >

    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >
    </ListView>

    <EditText
        android:id="@+id/mile"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_marginTop="220dp"
        android:ems="10" />

    <EditText
        android:id="@+id/litre"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/mile"
        android:layout_below="@+id/mile"
        android:ems="10" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/add"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/litre"
        android:layout_below="@+id/litre"
        android:layout_marginTop="19dp"
        android:text="Add New Record" />

</RelativeLayout>

record.java

package com.example.sgdriverdiary;


import java.text.SimpleDateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleCursorAdapter;

public class Record extends Activity implements View.OnClickListener,AdapterView.OnItemClickListener{
    EditText Text1;
    EditText Text2;
    Button Add;
    ListView List;

    MySQLiteHelper mHelper;
    SQLiteDatabase mDb;
    Cursor mCursor;
    SimpleCursorAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState){
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_record);
        Text1 = (EditText)findViewById(R.id.mile);
        Text2 = (EditText)findViewById(R.id.litre);
        Add = (Button)findViewById(R.id.add);
        Add.setOnClickListener(this);
        List = (ListView)findViewById(R.id.list);
        List.setOnItemClickListener(this);
        mHelper = new MySQLiteHelper(this);
    }
    public void onResumer(){
        super.onResume();
        mDb = mHelper.getWritableDatabase();
        String[] columns = new String[] {"_id", MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
        mCursor = mDb.query(MySQLiteHelper.TABLE_NAME, columns, null, null, null, null, null);
        String[] headers = new String[] {MySQLiteHelper.COL_DATE, MySQLiteHelper.COL_MILE, MySQLiteHelper.COL_LITRE};
        mAdapter = new  SimpleCursorAdapter(this, android.R.layout.two_line_list_item,mCursor, headers, new int[]{android.R.id.text1, android.R.id.text2});
        List.setAdapter(mAdapter);
        }
    public void onPause(){
        super.onPause();
        mDb.close();
        mCursor.close();
    }


    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        // TODO Auto-generated method stub
        mCursor.moveToPosition(position);
        String rowId = mCursor.getString(0);
        mDb.delete(MySQLiteHelper.TABLE_NAME, "_id = ?", new String[]{rowId});
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public void onClick(View v) {
        // TODO Auto-generated method stub
        ContentValues cv = new ContentValues(3);
        cv.put(MySQLiteHelper.COL_MILE, Text1.getText().toString());
        cv.put(MySQLiteHelper.COL_LITRE, Text2.getText().toString());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        cv.put(MySQLiteHelper.COL_DATE, dateFormat.format(new Date()));
        mDb.insert(MySQLiteHelper.TABLE_NAME, null,cv);
        mCursor.requery();
        mAdapter.notifyDataSetChanged();
        Text1.setText(null);
        Text2.setText(null);

    }

}

logcart错误

06-15 07:34:50.190: W/dalvikvm(3486): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
06-15 07:34:50.240: E/AndroidRuntime(3486): FATAL EXCEPTION: main
06-15 07:34:50.240: E/AndroidRuntime(3486): java.lang.NullPointerException
06-15 07:34:50.240: E/AndroidRuntime(3486):     at com.example.sgdriverdiary.Record.onClick(Record.java:75)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.view.View.performClick(View.java:3511)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.view.View$PerformClick.run(View.java:14105)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.os.Handler.handleCallback(Handler.java:605)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.os.Handler.dispatchMessage(Handler.java:92)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.os.Looper.loop(Looper.java:137)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at android.app.ActivityThread.main(ActivityThread.java:4424)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at java.lang.reflect.Method.invokeNative(Native Method)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at java.lang.reflect.Method.invoke(Method.java:511)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
06-15 07:34:50.240: E/AndroidRuntime(3486):     at dalvik.system.NativeStart.main(Native Method)

2 个答案:

答案 0 :(得分:1)

由于mDb值为null导致异常,您刚刚在onResumer方法中对其进行了初始化。我认为你在这个方法的名义上犯了一个拼写错误,所以替换

public void onResumer(){ 

@Override
public void onResume(){

答案 1 :(得分:0)

您应该将onResumer()重命名为onResume()