APP AIM 当我点击登录按钮时,它应该保存姓名&密码在数据库中,然后它应该显示一个新的活动,在点击时显示它应该从数据库中获取数据并在提供的Textview中显示它。当单击下一个按钮时,它应该在textview中显示下一个数据行。
请求 我是Android的新手请帮帮我。谢谢。
主要活动
public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb = new DatabaseHelper(getBaseContext());
EditText et_Username, et_Password;
Button bt_Login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//INITIALISING VIEWS
et_Username = (EditText) findViewById(R.id.etUsername);
et_Password = (EditText) findViewById(R.id.etPassword);
bt_Login = (Button) findViewById(R.id.btLogin);
//IMPLEMENTING ON CLICK LISTENER
bt_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name,pass;
name = et_Username.getText().toString();
pass = et_Password.getText().toString();
myDb.Insert(name, pass);
Intent i = new Intent(MainActivity.this,DataActivity.class);
startActivity(i);
}
});
}
}
DATAHELPER
公共类DatabaseHelper扩展了SQLiteOpenHelper {
public static final String DATABASE_NAME = "mydatabase.db";
public static final String TABLE_NAME = "Details";
public static final String COL_1 = "NAME";
public static final String COL_2 = "PASSWORD";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("CREATE TABLE " + TABLE_NAME + " (NAME TEXT PRIMARY KEY, PASSWORD TEXT) ");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
onCreate(db);
}
public boolean Insert(String name, String password){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(COL_1, name);
cv.put(COL_2, password);
long res = db.insert(TABLE_NAME, null, cv);
if(res == -1)
return false;
else
return true;
}
public Cursor getAllData()
{
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery("SELECT * FROM " + TABLE_NAME, null);
return cursor;
}
}
数据活动
public class DataActivity extends Activity {
DatabaseHelper myDb = new DatabaseHelper(getBaseContext());
TextView tv_Name,tv_Password;
Button bt_Ok,bt_Next;
String showName,showPass;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
//Initialising Views
tv_Name = (TextView) findViewById(R.id.tvShowUsername);
tv_Password = (TextView) findViewById(R.id.tvShowPassword);
bt_Ok = (Button) findViewById(R.id.btOk);
bt_Next = (Button) findViewById(R.id.btNxt);
bt_Ok.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = myDb.getAllData();
cursor.moveToFirst();
if(cursor != null){
showName = cursor.getString(0);
showPass = cursor.getString(1);
show(showName,showPass);
}
}
});
bt_Next.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Cursor cursor = myDb.getAllData();
cursor.moveToNext();
if(cursor!=null){
showName = cursor.getString(0);
showPass = cursor.getString(1);
show(showName,showPass);
}
}
});
}
public void show(String n,String p){
tv_Name.setText(n);
tv_Password.setText(p);
}
}
异常
9-16 13:56:09.939 9508-9514/? E/jdwp﹕ Failed sending reply to debugger: Broken pipe
09-16 13:56:19.679 9508-9508/com.test.anu.test E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.test.anu.test, PID: 9508
java.lang.NullPointerException
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.test.anu.test.DatabaseHelper.Insert(DatabaseHelper.java:39)
at com.test.anu.test.MainActivity$1.onClick(MainActivity.java:36)
at android.view.View.performClick(View.java:4438)
at android.view.View$PerformClick.run(View.java:18422)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:1)
你的问题是,getBaseContext()在调用onCreate()之前返回null,你的活动应该如下所示,
public class MainActivity extends ActionBarActivity {
DatabaseHelper myDb;
EditText et_Username, et_Password;
Button bt_Login;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myDb = new DatabaseHelper(getBaseContext());
//INITIALISING VIEWS
et_Username = (EditText) findViewById(R.id.etUsername);
et_Password = (EditText) findViewById(R.id.etPassword);
bt_Login = (Button) findViewById(R.id.btLogin);
//IMPLEMENTING ON CLICK LISTENER
bt_Login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name,pass;
name = et_Username.getText().toString();
pass = et_Password.getText().toString();
myDb.Insert(name, pass);
Intent i = new Intent(MainActivity.this,DataActivity.class);
startActivity(i);
}
});
}
}
答案 1 :(得分:0)
在Activity的 oncreate 方法中初始化您的db类。
DatabaseHelper myDb = new DatabaseHelper(getBaseContext());