我正在创建一个注册页面,当我输入不同的密码时,toast工作得很好。但是当我输入相同的密码时。 MainActivity未被重定向到yaya活动。它只是一个消息崩溃"应用程序停止工作"我再次被重定向到我的主要活动。
package com.example.abcd.helloworld;
import....
public class SignUp extends Activity {
DatabaseHelper helper = new DatabaseHelper(this);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.signup);
Button button2 = (Button) findViewById(R.id.Bsignupbutton);
button2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.Bsignupbutton) {
EditText name = (EditText) findViewById(R.id.TFname);
EditText email = (EditText) findViewById(R.id.email);
EditText uname = (EditText) findViewById(R.id.TFuname);
EditText pass1 = (EditText) findViewById(R.id.password);
EditText pass2 = (EditText) findViewById(R.id.pass2);
String namestr = name.getText().toString();
String emailstr = email.getText().toString();
String unamestr = uname.getText().toString();
String pass1str = pass1.getText().toString();
String pass2str = pass2.getText().toString();
if (!pass1str.equals(pass2str)) {
//popup msg
Toast pass = Toast.makeText(SignUp.this, "Passwords do not match!", Toast.LENGTH_SHORT);
pass.show();
} else {
User u = new User();
u.setName(namestr);
u.setEmail(emailstr);
u.setUname(unamestr);
u.setPass(pass1str);
helper.insertUser(u);
Intent i = new Intent(SignUp.this, yaya.class);
startActivity(i);
}
}
}
});
}
}
错误
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: FATAL EXCEPTION: main
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: android.database.sqlite.SQLiteException: near "mull": syntax error (code 1): , while compiling: create table contacts (id integer primary key not mull auto_increment,name text not null, email text not null, uname text not null, pass text not null
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:889)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:500)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.executeSql(SQLiteDatabase.java:1672)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteDatabase.execSQL(SQLiteDatabase.java:1603)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at com.example.sanchit.helloworld.DatabaseHelper.onCreate(DatabaseHelper.java:30)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:252)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at com.example.sanchit.helloworld.DatabaseHelper.insertUser(DatabaseHelper.java:36)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at com.example.sanchit.helloworld.SignUp$1.onClick(SignUp.java:51)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.view.View.performClick(View.java:4240)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.view.View$PerformClick.run(View.java:17721)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.os.Handler.handleCallback(Handler.java:730)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.os.Handler.dispatchMessage(Handler.java:92)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.os.Looper.loop(Looper.java:137)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at android.app.ActivityThread.main(ActivityThread.java:5103)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at java.lang.reflect.Method.invokeNative(Native Method)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at java.lang.reflect.Method.invoke(Method.java:525)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:737)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:553)
02-04 01:56:55.953 1822-1822/? E/AndroidRuntime: at dalvik.system.NativeStart.main(Native Method)
DatabaseHelper.java 包com.example.abcd.helloworld;
import...
public class DatabaseHelper extends SQLiteOpenHelper {
private static final int DATABASE_VERSION=1;
private static final String DATABASE_NAME="user.db";
private static final String TABLE_NAME="user";
private static final String COLUMN_ID="id";
private static final String COLUMN_NAME="name";
private static final String COLUMN_EMAIL="email";
private static final String COLUMN_UNAME="uname";
private static final String COLUMN_PASS="pass";
SQLiteDatabase db;
private static final String TABLE_CREATE="create table contacts (id integer primary key not null autoincrement,"+
"name text not null, email text not null, uname text not null, pass text not null );";
public DatabaseHelper(Context context)
{
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
public void insertUser(User u)
{
db = this.getWritableDatabase();
ContentValues values=new ContentValues();
String query="select * from user";
Cursor cursor= db.rawQuery(query, null);
int count= cursor.getCount();
values.put(COLUMN_ID, count);
values.put(COLUMN_NAME, u.getName());
values.put(COLUMN_EMAIL, u.getEmail());
values.put(COLUMN_UNAME, u.getUname());
values.put(COLUMN_PASS, u.getPass());
db.insert(TABLE_NAME, null, values);
db.close();
}
public String searchPass(String uname)
{
db=this.getReadableDatabase();
String query="select uname, pass from" +TABLE_NAME;
Cursor cursor= db.rawQuery(query,null);
String a,b;
b="not found";
if (cursor.moveToFirst())
{
do {
a= cursor.getString(0);
if (a.equals(uname))
{
b= cursor.getString(1);
break;
}
}
while(cursor.moveToNext());
}
db.close();
return b;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
String query= "DROP TABLE IF EXISTS"+ TABLE_NAME;
db.execSQL(query);
this.OnCreate(db);
}
private void OnCreate(SQLiteDatabase db) {
db.execSQL(TABLE_CREATE);
this.db = db;
}
}
SQLiteProgram.java
package android.database.sqlite;
import android.database.DatabaseUtils;
import android.os.CancellationSignal;
import java.util.Arrays;
public abstract class SQLiteProgram extends SQLiteClosable {
private static final String[] EMPTY_STRING_ARRAY = new String[0];
private final SQLiteDatabase mDatabase;
private final String mSql;
private final boolean mReadOnly;
private final String[] mColumnNames;
private final int mNumParameters;
private final Object[] mBindArgs;
SQLiteProgram(SQLiteDatabase db, String sql, Object[] bindArgs,
CancellationSignal cancellationSignalForPrepare) {
mDatabase = db;
mSql = sql.trim();
int n = DatabaseUtils.getSqlStatementType(mSql);
switch (n) {
case DatabaseUtils.STATEMENT_BEGIN:
case DatabaseUtils.STATEMENT_COMMIT:
case DatabaseUtils.STATEMENT_ABORT:
mReadOnly = false;
mColumnNames = EMPTY_STRING_ARRAY;
mNumParameters = 0;
break;
default:
boolean assumeReadOnly = (n == DatabaseUtils.STATEMENT_SELECT);
SQLiteStatementInfo info = new SQLiteStatementInfo();
db.getThreadSession().prepare(mSql,db.getThreadDefaultConnectionFlags(assumeReadOnly),cancellationSignalForPrepare, info);
mReadOnly = info.readOnly;
mColumnNames = info.columnNames;
mNumParameters = info.numParameters;
break;
}
if (bindArgs != null && bindArgs.length > mNumParameters) {
throw new IllegalArgumentException("Too many bind arguments. "
+ bindArgs.length + " arguments were provided but the statement needs "
+ mNumParameters + " arguments.");
}
if (mNumParameters != 0) {
mBindArgs = new Object[mNumParameters];
if (bindArgs != null) {
System.arraycopy(bindArgs, 0, mBindArgs, 0, bindArgs.length);
}
} else {
mBindArgs = null;
}
}
final SQLiteDatabase getDatabase() {
return mDatabase;
}
final String getSql() {
return mSql;
}
final Object[] getBindArgs() {
return mBindArgs;
}
final String[] getColumnNames() {
return mColumnNames;
}
/** @hide */
protected final SQLiteSession getSession() {
return mDatabase.getThreadSession();
}
/** @hide */
protected final int getConnectionFlags() {
return mDatabase.getThreadDefaultConnectionFlags(mReadOnly);
}
/** @hide */
protected final void onCorruption() {
mDatabase.onCorruption();
}
/**
* Unimplemented.
* @deprecated This method is deprecated and must not be used.
*/
@Deprecated
public final int getUniqueId() {
return -1;
}
public void bindNull(int index) {
bind(index, null);
}
/**
* Bind a long value to this statement. The value remains bound until
* {@link #clearBindings} is called.
*addToBindArgs
* @param index The 1-based index to the parameter to bind
* @param value The value to bind
*/
public void bindLong(int index, long value) {
bind(index, value);
}
/**
* Bind a double value to this statement. The value remains bound until
* {@link #clearBindings} is called.
*
* @param index The 1-based index to the parameter to bind
* @param value The value to bind
*/
public void bindDouble(int index, double value) {
bind(index, value);
}
/**
* Bind a String value to this statement. The value remains bound until
* {@link #clearBindings} is called.
*
* @param index The 1-based index to the parameter to bind
* @param value The value to bind, must not be null
*/
public void bindString(int index, String value) {
if (value == null) {
throw new IllegalArgumentException("the bind value at index " + index + " is null");
}
bind(index, value);
}
/**
* Bind a byte array value to this statement. The value remains bound until
* {@link #clearBindings} is called.
*
* @param index The 1-based index to the parameter to bind
* @param value The value to bind, must not be null
*/
public void bindBlob(int index, byte[] value) {
if (value == null) {
throw new IllegalArgumentException("the bind value at index " + index + " is null");
}
bind(index, value);
}
/**
* Clears all existing bindings. Unset bindings are treated as NULL.
*/
public void clearBindings() {
if (mBindArgs != null) {
Arrays.fill(mBindArgs, null);
}
}
/**
* Given an array of String bindArgs, this method binds all of them in one single call.
*
* @param bindArgs the String array of bind args, none of which must be null.
*/
public void bindAllArgsAsStrings(String[] bindArgs) {
if (bindArgs != null) {
for (int i = bindArgs.length; i != 0; i--) {
bindString(i, bindArgs[i - 1]);
}
}
}
@Override
protected void onAllReferencesReleased() {
clearBindings();
}
private void bind(int index, Object value) {
if (index < 1 || index > mNumParameters) {
throw new IllegalArgumentException("Cannot bind argument at index "
+ index + " because the index is out of range. "
+ "The statement has " + mNumParameters + " parameters.");
}
mBindArgs[index - 1] = value;
}
}
答案 0 :(得分:1)
android.database.sqlite.SQLiteException: near "mull": syntax error (code 1): , while compiling: create table contacts (id integer primary key not mull auto_increment
自动增量PK列的正确声明为integer primary key autoincrement
,而不是integer primary key not mull auto_increment
。您在null
中输入了拼写错误,并且不需要not null
。 sqlite autoincrement
中没有下划线。
答案 1 :(得分:0)
在DatabaseHelper helper = new DatabaseHelper(this);
onCreate
移至setContentView(R.layout.signup);
修改强>
根据您的logcat,您的CREATE
语句中似乎存在拼写错误。您的拼写null
为mull
。也请写AUTOINCREMENT
而不是auto_increment
。
答案 2 :(得分:0)
创建Helper类的全局变量。
DatabaseHelper helper;
然后将此代码添加到 onCreate
helper = new DatabaseHelper(SignUp.this);