我在做什么:: 我正在尝试创建数据库并将值插入其中的表
我面临的问题:: 我的数据库没有创建(我在DDMS中检查过)
MainActivity.java
public class MainActivity extends Activity implements OnClickListener {
Button addUser;
EditText name,pwd;
DatabaseAdapter adapter;
Context context;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
addUser=(Button) findViewById(R.id.button1);
name=(EditText) findViewById(R.id.editText1);
pwd=(EditText) findViewById(R.id.editText2);
}
@Override
protected void onStart() {
// TODO Auto-generated method stub
super.onStart();
addUser.setOnClickListener(this);
}
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch(v.getId()){
case R.id.button1:
long returnedRes = 0;
try {
returnedRes = adapter.insertData(name.getText().toString(), pwd.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Log-EXCEPTION", e.toString());
}
if(returnedRes<0)
{
Message.message(this, "Unsuccessful");
Log.d("LOG-MSG", "Unsuccessful");
}else{
Message.message(this, "Successful");
Log.d("LOG-MSG", "Successful");
}
break;
}
}
}
DatabaseAdapter.java
public class DatabaseAdapter {
DatabaseHelper helper;
ContentValues cv;
SQLiteDatabase db;
//Constructor of outer class
DatabaseAdapter(Context context){
helper=new DatabaseHelper(context);//creating object of Inner class
}
public long insertData(String name,String password){
db=helper.getWritableDatabase();
cv.put(helper.NAME, name);
cv.put(helper.PASSWORD, password);
long id=db.insert(helper.TABLE_NAME, null, cv);
/*
* -1 if the value is inserted has some problem
* else it will return the rowId of the column inserted
*/
return id;
}
class DatabaseHelper extends SQLiteOpenHelper{
private Context context;
private static final String DATABASE_NAME="MyDatabase";
private static final String TABLE_NAME="MyTable";
private static final int DATABASE_VERSION=1;
private static final String ID="_id";
private static final String NAME="name";
private static final String PASSWORD="password";
private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INT PRIMARY KEY AUTO_INCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
private static final String DROP_TABLE="DROP TABLE IF EXISTS "+TABLE_NAME+"";
public DatabaseHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
this.context=context;
Message.message(context, "Constructor \n called");
Log.d("LOG-MSG", "Constructor \n called");
}
@Override
public void onCreate(SQLiteDatabase db) {
//CREATE TABLE MyTable(_id INT PRIMARY KEY AUTO_INCREMENT, name VARCHAR(225));
try {
Message.message(context, "onCreate \n called");
Log.d("LOG-MSG", "onCreate \n called");
db.execSQL(CREATE_TABLE);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
//DROP TABLE MyTable IF EXISTS;
try {
Message.message(context, "onUpgrade \n called");
Log.d("LOG-MSG", "onUpgrade \n called");
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
Message.message(context, ""+e);
}
}
}
}
日志 ::
04-18 09:47:51.250: W/ActivityThread(6470): Application com.example.sqliteslidenerdproject is waiting for the debugger on port 8100...
04-18 09:47:51.350: I/System.out(6470): Sending WAIT chunk
04-18 09:47:51.600: I/dalvikvm(6470): Debugger is active
04-18 09:47:51.790: I/System.out(6470): Debugger has connected
04-18 09:47:51.790: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:51.990: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.191: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.390: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.600: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:52.800: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.002: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.202: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.410: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.610: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:53.820: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.020: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.220: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.420: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.630: I/System.out(6470): waiting for debugger to settle...
04-18 09:47:54.830: I/System.out(6470): debugger has settled (1427)
04-18 09:48:06.050: D/gralloc_goldfish(6470): Emulator without GPU emulation detected.
04-18 09:48:28.411: W/System.err(6470): java.lang.NullPointerException
04-18 09:48:28.441: W/System.err(6470): at com.example.sqliteslidenerdproject.MainActivity.onClick(MainActivity.java:47)
04-18 09:48:28.600: W/System.err(6470): at android.view.View.performClick(View.java:3480)
04-18 09:48:28.633: W/System.err(6470): at android.view.View$PerformClick.run(View.java:13983)
04-18 09:48:28.650: W/System.err(6470): at android.os.Handler.handleCallback(Handler.java:605)
04-18 09:48:28.661: W/System.err(6470): at android.os.Handler.dispatchMessage(Handler.java:92)
04-18 09:48:28.681: W/System.err(6470): at android.os.Looper.loop(Looper.java:137)
04-18 09:48:28.702: W/System.err(6470): at android.app.ActivityThread.main(ActivityThread.java:4340)
04-18 09:48:28.721: W/System.err(6470): at java.lang.reflect.Method.invokeNative(Native Method)
04-18 09:48:28.741: W/System.err(6470): at java.lang.reflect.Method.invoke(Method.java:511)
04-18 09:48:28.761: W/System.err(6470): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-18 09:48:28.791: W/System.err(6470): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-18 09:48:28.801: W/System.err(6470): at dalvik.system.NativeStart.main(Native Method)
04-18 09:48:28.821: D/Log-EXCEPTION(6470): java.lang.NullPointerException
04-18 09:48:32.651: D/LOG-MSG(6470): Successful
答案 0 :(得分:3)
我认为您忘记初始化DatabaseAdapter adapter
,如下所示:
DatabaseAdapter adapter=new DatabaseAdapter(MainActivity.this);
您的应用程序崩溃了,因为适配器在您null
事件的adapter.insertData(.....)
处Button Click
处Create Table SQL command
。
更新:您在"CREATE TABLE "+TABLE_NAME+"("+ID+" INT PRIMARY KEY AUTO_INCREMENT // Remove _ from AUTO_INCREMENT from after primary key and also change INT to INTEGER
Table SQL command
更正您的创建 private static final String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+"("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+NAME+" VARCHAR(225), "+PASSWORD+" VARCHAR(225));";
及以下
{{1}}
答案 1 :(得分:2)
我认为您忘记初始化adapter
变量。这就是你得到NULL POINTER Exception
的原因。尝试初始化它并再次运行。
在onCreate()
中添加此行adapter = new DatabaseAdapter(MainActivity.this);
答案 2 :(得分:1)
我认为适配器为null,因为您尚未创建DatabaseAdapter类的实例
尝试以下代码。
try {
adapter = new DatabaseAdapter(MainActivity.this);
returnedRes = adapter.insertData(name.getText().toString(), pwd.getText().toString());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
Log.d("Log-EXCEPTION", e.toString());
}