我正在尝试创建一个需要注册表单的Android应用程序。我已经做了一些事情,但现在的问题是,当用户输入密码和确认密码相同时,为了点击注册按钮,应用程序停止不幸。但是当我提供不同的密码时,它运行良好。 以下是我的代码:
TableData.java
import android.provider.BaseColumns;
public class TableData {
public TableData()
{
}
public static abstract class TableInfo implements BaseColumns
{
public static final String USER_NAME = "user_name";
public static final String USER_PASS = "user_pass";
public static final String DATABASE_NAME = "user_info";
public static final String TABLE_NAME = "reg_info";
}
}
DatabaseOperations.java
import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseOperations extends SQLiteOpenHelper{
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE_TABLE" + TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT );" ;
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
Log.d("Database operations", "Database Created");
}
@Override
public void onCreate(SQLiteDatabase sdb) {
sdb.execSQL(CREATE_QUERY);
Log.d("Database operations", "Table Created");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
}
public void putInformation(DatabaseOperations dop, String name, String pass)
{
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME, null, cv);
Log.d("Database operations", "One Raw Inserted");
}
}
和register.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class register extends AppCompatActivity {
EditText USER_NAME, USER_PASS, CON_PASS;
String user_name, user_pass, con_pass;
Button REG;
Context ctx = this;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText) findViewById(R.id.reg_pass);
CON_PASS = (EditText) findViewById(R.id.con_pass);
REG = (Button) findViewById(R.id.user_reg);
REG.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
if (!(user_pass.equals(con_pass)))
{
Toast.makeText(getBaseContext(),"Passwords are not matching",Toast.LENGTH_LONG).show();
USER_NAME.setText("");
USER_PASS.setText("");
CON_PASS.setText("");
}
else
{
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB, user_name, user_pass);
Toast.makeText(getBaseContext(),"Registration is Successfully done",Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}
请给我一些建议如何解决,谢谢!
答案 0 :(得分:0)
你创建表的sql是错误的,替换
public String CREATE_QUERY = "CREATE_TABLE" + TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT );" ;
与
public String CREATE_QUERY = "CREATE TABLE" + TableData.TableInfo.TABLE_NAME+"("+ TableData.TableInfo.USER_NAME+" TEXT,"+ TableData.TableInfo.USER_PASS+" TEXT );" ;
答案 1 :(得分:0)
这应该适合你,我自己做了。
TableData.java
import android.provider.BaseColumns;
public class TableData {
public TableData() {
}
public static abstract class TableInfo implements BaseColumns
{
public static final String USER_NAME = "user_name";
public static final String USER_PASS = "user_password";
public static final String DATABASE_NAME = "user_info";
public static final String TABLE_NAME = "reg_info";
}
}
DatabaseOperations.java
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DatabaseOperations extends SQLiteOpenHelper {
public static final int database_version = 1;
public String CREATE_QUERY = "CREATE TABLE "+ TableData.TableInfo.TABLE_NAME+
"(" + TableData.TableInfo.USER_NAME+ " TEXT," + TableData.TableInfo.USER_PASS+ " TEXT );";
public DatabaseOperations(Context context) {
super(context, TableData.TableInfo.DATABASE_NAME, null, database_version);
Log.d("MYLOG", "database created");
}
public void onCreate(SQLiteDatabase sdb){
sdb.execSQL(CREATE_QUERY);
Log.d("MYLOG", "table created");
}
public void onUpgrade(SQLiteDatabase arg0,int arg1,int arg2){
}
public void putInformation(DatabaseOperations dop,String name,String pass){
SQLiteDatabase sq = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME,name);
cv.put(TableData.TableInfo.USER_PASS, pass);
long k = sq.insert(TableData.TableInfo.TABLE_NAME,null,cv);
Log.d("MYLOG", "one row inserted" + k);
}
}
Register.java
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class RegisterActivity extends Activity{
EditText USER_NAME,USER_PASS,CON_PASS;
String user_name, user_pass,con_pass;
Button REG;
Context ctx = this;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.register_layout);
USER_NAME = (EditText) findViewById(R.id.reg_user);
USER_PASS = (EditText)findViewById(R.id.reg_pass);
CON_PASS = (EditText)findViewById(R.id.con_pass);
REG = (Button)findViewById(R.id.user_reg);
REG.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
user_name = USER_NAME.getText().toString();
user_pass = USER_PASS.getText().toString();
con_pass = CON_PASS.getText().toString();
if(!(user_pass.equals(con_pass))){
Toast.makeText(getBaseContext(),"Passwords are not matching", Toast.LENGTH_LONG).show();
USER_NAME.setText("");
USER_PASS.setText("");
CON_PASS.setText("");
}else{
DatabaseOperations DB = new DatabaseOperations(ctx);
DB.putInformation(DB,user_name,user_pass);
Toast.makeText(getBaseContext(),"Registration success", Toast.LENGTH_LONG).show();
finish();
}
}
});
}
}