在Cursor cursor = mDb.query(TABLENAME, null, KEY_ID + "=" + uid, null, null,null, null, null);中加入KEY_ID + "=" + uid运行就会出现"the application has stropped unexpectedly,please try again";若将条件去掉,改为null,就能正常跳转到information页面,不知道代码哪里写错了,源代码如下:
public class Jtest extends Activity {
Button btn;
EditText txt1,txt2;
String uid,pwd;
private static final String DATABASENAME="Test.db";
private static final String TABLENAME="Ttable";
private static final int VERSION=1;
private static final String KEY_ID="userid";
private static final String KEY_PASSWORD="password";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static String sql = "CREATE TABLE " + TABLENAME + " (" + KEY_ID
+ " text not null, " + KEY_PASSWORD + " text not null " + ");";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASENAME, null, VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql); } @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
onCreate(db);
}
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DatabaseHelper(this);
mDb= mDbHelper.getWritableDatabase();
String sql1 = "insert into " + TABLENAME + " (" + KEY_ID + ", " + KEY_PASSWORD
+ ") values('admin', 'admin');";
mDb.execSQL(sql1);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt1=(EditText)findViewById(R.id.userid);
txt2=(EditText)findViewById(R.id.password);
uid=txt1.getText().toString();
pwd=txt2.getText().toString();
mDb = mDbHelper.getReadableDatabase();
Cursor cursor = mDb.query(TABLENAME, null, KEY_ID + "=" + uid, null, null,
null, null, null);
if(cursor.getCount()>0)
{
Intent intent=new Intent();
intent.setClass(Jtest.this, Information.class);
startActivity(intent);
}
else {
Toast.makeText(Jtest.this, "UserId or Password error!", Toast.LENGTH_LONG).show();
}
}
});
}}
public class Jtest extends Activity {
Button btn;
EditText txt1,txt2;
String uid,pwd;
private static final String DATABASENAME="Test.db";
private static final String TABLENAME="Ttable";
private static final int VERSION=1;
private static final String KEY_ID="userid";
private static final String KEY_PASSWORD="password";
private DatabaseHelper mDbHelper;
private SQLiteDatabase mDb;
private static String sql = "CREATE TABLE " + TABLENAME + " (" + KEY_ID
+ " text not null, " + KEY_PASSWORD + " text not null " + ");";
private static class DatabaseHelper extends SQLiteOpenHelper {
DatabaseHelper(Context context) {
super(context, DATABASENAME, null, VERSION);
} @Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(sql); } @Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLENAME);
onCreate(db);
}
} @Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mDbHelper = new DatabaseHelper(this);
mDb= mDbHelper.getWritableDatabase();
String sql1 = "insert into " + TABLENAME + " (" + KEY_ID + ", " + KEY_PASSWORD
+ ") values('admin', 'admin');";
mDb.execSQL(sql1);
btn=(Button)findViewById(R.id.btn);
btn.setOnClickListener(new Button.OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
txt1=(EditText)findViewById(R.id.userid);
txt2=(EditText)findViewById(R.id.password);
uid=txt1.getText().toString();
pwd=txt2.getText().toString();
mDb = mDbHelper.getReadableDatabase();
Cursor cursor = mDb.query(TABLENAME, null, KEY_ID + "=" + uid, null, null,
null, null, null);
if(cursor.getCount()>0)
{
Intent intent=new Intent();
intent.setClass(Jtest.this, Information.class);
startActivity(intent);
}
else {
Toast.makeText(Jtest.this, "UserId or Password error!", Toast.LENGTH_LONG).show();
}
}
});
}}
解决方案 »
- android中怎样从xml文件中解析一个二维数组
- 为什么我的MotionEvent.ACTION_UP事件没法被响应呢?
- 为什么我导入sdk的demo,com.example.android.apis.R找不到呢?
- Android环境,cmwap接入,https速度过慢问题讨论
- 获得utf-8网页后,搜索中文的问题
- 想学Android平台开发,请教一下?
- 【紧急求助】一个程序不能设置俩闹钟广播么?
- 如果更改android 4.4.4上的launcher3默认布局
- 读取sdcard中的txt文档
- 无聊写的一个Tree控件
- android 使用ksoap2-android-assembly-2.4-jar如何设置连接webservice的超时问题
- android2.3自带的锁屏程序源码文件是哪个啊?
还有,你把你的数据库封装的功能全一点啊。增删改查,关闭一个都不能少,反正以后都可以用的。