将用户注册数据保存到数据库

时间:2014-04-06 11:45:25

标签: android database registration

我试图将数据输入注册表单以保存到数据库中。 这是我的数据库:     包com.NebulaNewsCo.gameofwin;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;

public class gowDbAdapter {

    public static final String KEY_USERNAME = "username";
    public static final String KEY_PASSWORD = "password";
    public static final String KEY_ROWID = "_id";

    private static final String TAG = "gowDbAdapter";
    private DatabaseHelper mDbHelper;
    private SQLiteDatabase mDb;

    /**
     * Database creation sql statement
     */
    private static final String DATABASE_CREATE =
        "create table notes (_id integer primary key autoincrement, "
        + "title text not null, body text not null);";

    private static final String DATABASE_NAME = "gow";
    private static final String DATABASE_TABLE = "users";
    private static final int DATABASE_VERSION = 2;

    private final Context mCtx;

    private static class DatabaseHelper extends SQLiteOpenHelper {

        DatabaseHelper(Context context) {
            super(context, DATABASE_NAME, null, DATABASE_VERSION);
        }

        @Override
        public void onCreate(SQLiteDatabase db) {

            db.execSQL(DATABASE_CREATE);
        }

        @Override
        public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
            Log.w(TAG, "Upgrading database from version " + oldVersion + " to "
                    + newVersion + ", which will destroy all old data");
            db.execSQL("DROP TABLE IF EXISTS notes");
            onCreate(db);
        }
    }

    /**
     * Constructor - takes the context to allow the database to be
     * opened/created
     * 
     * @param ctx the Context within which to work
     */
    public gowDbAdapter(Context ctx) {
        this.mCtx = ctx;
    }

    /**
     * Open the notes database. If it cannot be opened, try to create a new
     * instance of the database. If it cannot be created, throw an exception to
     * signal the failure
     * 
     * @return this (self reference, allowing this to be chained in an
     *         initialization call)
     * @throws SQLException if the database could be neither opened or created
     */
    public gowDbAdapter open() throws SQLException {
        mDbHelper = new DatabaseHelper(mCtx);
        mDb = mDbHelper.getWritableDatabase();
        return this;
    }

    public void close() {
        mDbHelper.close();
    }


    /**
     * @param username
     * @param password
     * @return rowId or -1 if failed
     */
    public long createUser(String username, String password) {
        ContentValues initialValues = new ContentValues();
        initialValues.put(KEY_USERNAME, username);
        initialValues.put(KEY_PASSWORD, password);

        return mDb.insert(DATABASE_TABLE, null, initialValues);
    }

    /**
     * Delete the user with the given rowId
     * 
     * @param rowId id of user to delete
     * @return true if deleted, false otherwise
     */
    public boolean deleteUser(long rowId) {

        return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
    }

    /**
     * Return a Cursor over the list of all users in the database
     * 
     * @return Cursor over all users
     */
    public Cursor fetchAllUsers() {

        return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_USERNAME,
                KEY_PASSWORD}, null, null, null, null, null);
    }

    /**
     * Return a Cursor positioned at the user that matches the given rowId
     * 
     * @param rowId id of user to retrieve
     * @return Cursor positioned to matching user, if found
     * @throws SQLException if note could not be found/retrieved
     */
    public Cursor fetchUser(long rowId) throws SQLException {

        Cursor mCursor =

            mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
                    KEY_USERNAME, KEY_USERNAME}, KEY_ROWID + "=" + rowId, null,
                    null, null, null, null);
        if (mCursor != null) {
            mCursor.moveToFirst();
        }
        return mCursor;

    }

    /**
     * Update the user using the details provided. The user to be updated is
     * specified using the rowId, and it is altered to use the username and password
     * values passed in
     * 
     * @param rowId id of user to update
     * @param username value to set users username to
     * @param password value to set users password to
     * @return true if the user was successfully updated, false otherwise
     */
    public boolean updateUser(long rowId, String username, String password) {
        ContentValues args = new ContentValues();
        args.put(KEY_USERNAME, username);
        args.put(KEY_PASSWORD, password);

        return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
    }
}

这是我的注册表单java      包com.NebulaNewsCo.gameofwin;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class Register extends Activity {
private EditText mUsernameText;
private EditText mPasswordText;
private Long mRowId;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    mUsernameText = (EditText) findViewById(R.id.reg_user_edit);
    mPasswordText = (EditText) findViewById(R.id.reg_pass_edit);

    Button confirmButton = (Button) findViewById(R.id.reg_submit_btn);

    mRowId = null;
    Bundle extras = getIntent() .getExtras();
    if (extras != null) {
        String title = extras.getString(gowDbAdapter.KEY_USERNAME);
        String body = extras.getString(gowDbAdapter.KEY_PASSWORD);
        mRowId = extras.getLong(gowDbAdapter.KEY_ROWID);

        if (title != null) {
            mUsernameText.setText(title);
        }
        if (body != null) {
            mPasswordText.setText(body);
        }
    }

    confirmButton.setOnClickListener(new View.OnClickListener() {
        public void onClick(View view) {
            Bundle bundle = new Bundle();

            bundle.putString(gowDbAdapter.KEY_USERNAME, mUsernameText.getText().toString());
            bundle.putString(gowDbAdapter.KEY_PASSWORD, mPasswordText.getText().toString());
            if (mRowId != null) {
                bundle.putLong(gowDbAdapter.KEY_ROWID, mRowId);
            }
            Intent mIntent = new Intent();
            mIntent.putExtras(bundle);
            setResult(RESULT_OK, mIntent);
            finish();
        }
    });
}


/** Called when the user clicks the Go Back button */
public void goBack(View view) {
    Intent intent = new Intent(Register.this, Home.class);
    startActivity(intent);
    finish();
}

/** Called when the user clicks the Submit button */
public void regSubmit(View view) { 
    Intent intent = new Intent(this, RegisterComplete.class);       
    startActivity(intent);
    finish();
}

}

当我在测试时打开注册活动时崩溃了。如果您需要更多信息,请告诉我。

编辑:

以下是错误日志:

04-06 13:58:59.102: E/Trace(4540): error opening trace file: No such file or directory (2)
04-06 13:58:59.540: I/dalvikvm-heap(4540): Grow heap (frag case) to 11.726MB for 5644816-byte allocation
04-06 13:58:59.842: I/dalvikvm-heap(4540): Grow heap (frag case) to 14.118MB for 2508816-byte allocation
04-06 13:59:00.417: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:00.644: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:00.977: W/MMUMapper(4540): fail to register MVA, unsupported format(0x5)
04-06 13:59:01.474: I/SurfaceTextureClient(4540): [0x51768fa8] frames:3,   duration:1.008000, fps:2.975705
04-06 13:59:02.975: I/SurfaceTextureClient(4540): [0x51768fa8] frames:3,   duration:1.500000, fps:1.999203
04-06 13:59:03.967: W/dalvikvm(4540): threadid=1: thread exiting with uncaught exception (group=0x40f3b908)
04-06 13:59:03.983: E/AndroidRuntime(4540): FATAL EXCEPTION: main
04-06 13:59:03.983: E/AndroidRuntime(4540): java.lang.RuntimeException: Unable to start  activity ComponentInfo{com.NebulaNewsCo.gameofwin/com.NebulaNewsCo.gameofwin.Register}:  java.lang.NullPointerException
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2203)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2230)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.access$600(ActivityThread.java:150)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1301)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.os.Handler.dispatchMessage(Handler.java:99)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.os.Looper.loop(Looper.java:153)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.main(ActivityThread.java:5006)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at java.lang.reflect.Method.invokeNative(Native Method)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at java.lang.reflect.Method.invoke(Method.java:511)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:821)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:584)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at dalvik.system.NativeStart.main(Native Method)
04-06 13:59:03.983: E/AndroidRuntime(4540): Caused by: java.lang.NullPointerException
04-06 13:59:03.983: E/AndroidRuntime(4540):     at com.NebulaNewsCo.gameofwin.Register.onCreate(Register.java:39)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.Activity.performCreate(Activity.java:5076)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1083)
04-06 13:59:03.983: E/AndroidRuntime(4540):     at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2167)
04-06 13:59:03.983: E/AndroidRuntime(4540):     ... 11 more

2 个答案:

答案 0 :(得分:1)

打开log cat并找到错误。

答案 1 :(得分:0)

在日志中,Register.java的第39行包含您操作的某个null值。

在消息来源中,这似乎就是这条线

confirmButton.setOnClickListener(new View.OnClickListener() {

看起来confirmButtonnull,导致NullPointerException被抛出,因为您尝试访问该对象。 findViewById返回null的原因是找不到请求的视图。

您的代码似乎没有设置任何内容视图(例如,使用setContentView),因此,您无法使用findViewById找到任何视图。