亲爱的我在我的主视图中有两个按钮“注册”和“登录”,当我点击注册然后下一个视图加载了两个文本框字段“用户名”和“密码”和一个提交按钮,当我点击按钮我正在通过buttonclick()事件中的finish()将数据发送到数据库并返回到我的主要活动。现在,当我点击登录按钮时,它打开下一个查看用户名和密码,当我填写它并单击提交按钮它强制关闭..我在我的内容提供商数据库中使用validate()方法...所以我无法找到bug请参阅我的代码,并帮助我...请在adv.and另一个问题是如何检查数据是否插入数据库...
我的内容提供商类是
public class MyContentProvider extends ContentProvider {
private SQLiteDatabase sqlDB;
public DatabaseHelper dbHelper;
private static final String DATABASE_NAME = "Users.db";
private static final int DATABASE_VERSION = 1;
private static final String TABLE_NAME = "User";
private static final String TAG = "MyContentProvider";
public static class DatabaseHelper extends SQLiteOpenHelper {
//public String USER_NAME;
//public String PASSWORDS;
DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// create table to store user names
db.execSQL("Create table "
+ TABLE_NAME
+ "( _id INTEGER PRIMARY KEY AUTOINCREMENT, USER_NAME TEXT,PASSWORDS TEXT);");
}
public boolean validateUser(String username, String password){
Cursor c = getReadableDatabase().rawQuery(
"SELECT * FROM " + TABLE_NAME + " WHERE "
+ MyUsers.User.USER_NAME + "='" + username +"'AND "+MyUsers.User.Pass+"='"+password+"'" , null);
if (c.getCount()>0)
return true;
return false;
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
}
@Override
public int delete(Uri uri, String s, String[] as) {
return 0;
}
@Override
public String getType(Uri uri) {
return null;
}
@Override
public Uri insert(Uri uri, ContentValues contentvalues) {
// get database to insert records
sqlDB = dbHelper.getWritableDatabase();
// insert record in user table and get the row number of recently inserted record
long rowId = sqlDB.insert(TABLE_NAME, "",contentvalues);
if (rowId > 0) {
Uri rowUri = ContentUris.appendId(
MyUsers.User.CONTENT_URI.buildUpon(), rowId).build();
getContext().getContentResolver().notifyChange(rowUri, null);
return rowUri;
}
throw new SQLException("Failed to insert row into " + uri);
}
@Override
public boolean onCreate() {
dbHelper = new DatabaseHelper(getContext());
return (dbHelper == null) ? false : true;
}
@Override
public Cursor query(Uri uri, String[] projection, String selection,
String[] selectionArgs, String sortOrder) {
SQLiteQueryBuilder qb = new SQLiteQueryBuilder();
SQLiteDatabase db = dbHelper.getReadableDatabase();
qb.setTables(TABLE_NAME);
Cursor c = qb.query(db, projection, selection, null, null, null,
sortOrder);
c.setNotificationUri(getContext().getContentResolver(), uri);
return c;
}
@Override
public int update(Uri uri, ContentValues contentvalues, String s,
String[] as) {
return 0;
}
MYUser课程
public class MyUsers {
public static DatabaseHelper dbHelper;
static final String TABLE_NAME = "User";
public static final String AUTHORITY = "com.linkwithweb.providers.MyContentProvider";
// BaseColumn contains _id.
public static final class User implements BaseColumns {
public static final Uri CONTENT_URI = Uri
.parse("content://com.linkwithweb.providers.MyContentProvider");
// Table column
public static final String USER_NAME = "USER_NAME";
public static final String Pass = "Pass";
}
登录页面是:
public class loginpage extends Activity
{
// DatabaseHelper dbHelper;
DatabaseHelper dbHelper=new DatabaseHelper(loginpage.this);
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginpage);
Button button3 = (Button) findViewById(R.id.button3);
button3.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etext = (EditText) findViewById(R.id.editText);
String Name=( etext.getText().toString());
EditText etext2 = (EditText) findViewById(R.id.editText3);
String Password =( etext2.getText().toString());
Log.i("saurabh ..",Name);
if (dbHelper.validateUser(Name,Password))
{
Intent i = new Intent(loginpage.this,welcome.class);
startActivity(i);
}
//else
//Toast.makeText(getBaseContext(), "Wrong username/password", Toast.LENGTH_LONG).show();
}
});
}
注册页面是:::
public class signform extends Activity
{
MyAdapter mAdapter=null;
DatabaseHelper dbHelper;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.loginform);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
EditText etext = (EditText) findViewById(R.id.editText1);
String Names=( etext.getText().toString());
EditText etext2 = (EditText) findViewById(R.id.editText3);
String Password =( etext2.getText().toString());
**mAdapter.insertRecord(Names,Password)**;
finish();
}
});
mAdapter = new MyAdapter(this);
}
class MyAdapter extends BaseAdapter implements OnClickListener
{
private LayoutInflater inflater;
public MyAdapter(Context ctx) {
super();
this.inflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
public void insertRecord(String unames,String Passw) {
ContentValues values = new ContentValues();
values.put(MyUsers.User.USER_NAME, unames);
values.put(MyUsers.User.Pass, Passw);
getContentResolver().insert(MyUsers.User.CONTENT_URI, values);
notifyDataSetChanged();
Log.i("check for data insertion.......", "data inserted succesfully");
}
请检查为什么在点击按钮时不执行登录...谢谢
请在修改
后查看我的日志猫07-01 12:17:32.911: ERROR/AndroidRuntime(600): Uncaught handler: thread main exiting due to uncaught exception
07-01 12:17:32.940: ERROR/AndroidRuntime(600): android.database.sqlite.SQLiteException: no such column: Pass: , while compiling: SELECT * FROM User WHERE USER_NAME='first names'AND Pass='last names'
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.native_compile(Native Method)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.compile(SQLiteProgram.java:110)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:59)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:49)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:49)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1220)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1193)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.linkwithweb.providers.MyContentProvider$DatabaseHelper.validateUser(MyContentProvider.java:45)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at loginapp.page.loginpage$1.onClick(loginpage.java:29)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.performClick(View.java:2364)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.onTouchEvent(View.java:4179)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.widget.TextView.onTouchEvent(TextView.java:6540)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.View.dispatchTouchEvent(View.java:3709)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewGroup.dispatchTouchEvent(ViewGroup.java:884)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow$DecorView.superDispatchTouchEvent(PhoneWindow.java:1659)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow.superDispatchTouchEvent(PhoneWindow.java:1107)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.app.Activity.dispatchTouchEvent(Activity.java:2061)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at com.android.internal.policy.impl.PhoneWindow$DecorView.dispatchTouchEvent(PhoneWindow.java:1643)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.view.ViewRoot.handleMessage(ViewRoot.java:1691)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.os.Handler.dispatchMessage(Handler.java:99)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.os.Looper.loop(Looper.java:123)
07-01 12:17:32.940: ERROR/AndroidRuntime(600): at android.app.ActivityThread.main(ActivityThread.java:4363)
答案 0 :(得分:2)
在您的登录页面活动中,您调用了dbHelper.validateUser(名称,密码)。但是您没有使用dbHelper。
写
dbHelper=new DatabaseHelper(this);
在致电dbHelper.validateUser();
当您创建表时,您将列定义为PASSWORD。那么您的列名是PASSWORD。但是当您进行查询时,您的查询就像
"SELECT * FROM " + TABLE_NAME + " WHERE "
+ MyUsers.User.USER_NAME + "='" + username +"'AND "+MyUsers.User.Pass+
MyUsers.User.Pass的值是Pass.not PASSWORD.so你有问题。请更正列名。希望你完成