您好我正在使用我的应用程序项目..只是在我点击登录按钮后,会弹出一条错误消息,说抱歉!应用程序意外停止。请再试一次。这有什么错误?请帮助谢谢
这是我的用户数据库代码..
package com.gomez.android;
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 dbuser{
public static final String user_name = "username";
public static final String user_password = "password";
public static final String row_id = "_id";
private static final String TAG = "UdbAdapter";
private DatabaseHelper UdbHelper;
private SQLiteDatabase Udb;
/**
* Database creation sql statement
*/
private static final String DATABASE_CREATE =
"create table pages (_id integer primary key autoincrement, "
+ "username text not null, password text not null);";
private static final String DATABASE_NAME = "UserAccount";
private static final String DATABASE_TABLE = "User";
private static final int DATABASE_VERSION = 1;
private Context context = null;
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 pages");
onCreate(db);
}
}
public dbuser(Context cntxt) {
this.context = cntxt;
UdbHelper = new DatabaseHelper(context);
}
public void open() throws SQLException {
Udb = UdbHelper.getWritableDatabase();
}
public void close() {
UdbHelper.close();
}
public long createaccount(String username, String password)
{
Udb = UdbHelper.getWritableDatabase();
ContentValues initialValues = new ContentValues();
initialValues.put(user_name, username);
initialValues.put(user_password, password);
return Udb.insert(DATABASE_TABLE, null, initialValues);
}
public boolean Login(String username, String password)throws SQLException
{
Cursor mCursor = Udb.rawQuery(DATABASE_TABLE, new String[]{username,password});
if (mCursor != null){
if(mCursor.getCount() > 0){
return true;
}
}
return false;
}
}
这是我的登录页面代码.....
package com.gomez.android;
import com.gomez.android.dbuser;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
public class login extends Activity{
//Declare views
private EditText uname;
private EditText pword;
private Button btnlogin;
private Button btncancel;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//set Activity Layout
setContentView(R.layout.login);
//Get EditText and Button References
uname = (EditText)findViewById(R.id.username);
pword = (EditText)findViewById(R.id.password);
btnlogin = (Button)findViewById(R.id.login_enter);
btncancel = (Button)findViewById(R.id.cancel);
//set Click Listener
btnlogin.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
//Check Login
final String username = uname.getText().toString();
final String password = pword.getText().toString();
dbuser users = new dbuser(login.this);
users.open();
if(users.Login(username, password)){
if(username.equalsIgnoreCase(username)&& password.equalsIgnoreCase(password))
{
Toast.makeText(login.this,"Successfully Logged In", Toast.LENGTH_LONG).show();
Intent i = new Intent(login.this, firstpage.class);
startActivity(i);
}
else{
Toast.makeText(login.this,"Invalid Username/Password", Toast.LENGTH_LONG).show();
}
users.close();
}
}
});
btncancel.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v){
//close application
finish();
}
});
}
}
答案 0 :(得分:0)
我无法确定您的代码存在什么问题,因为您没有从LogCat提供任何信息,但getWriteableDatabase()
可能在运行时返回缓存的数据库应用程序(我在第一次实现和测试数据库时经常遇到这个问题。)
要确保这不是问题,请转到设置 - >应用程序 - >管理应用程序 - > [您的申请] - >在从Eclipse运行应用程序之前清除数据。这是一种简单的方法,可确保您在实施/测试数据库的早期阶段使用最新的实施。
这很可能不是问题所在,但这是你不会期望成为问题的事情之一(通常会导致数小时的挫折),所以我想我会带来它起来:)。