我得到一个空指针异常,当我点击确认按钮将数据保存到sqlite数据库时我的应用程序关闭。我真的不知道这里有什么问题...
public class Morfologia extends Activity {
DatabaseAdapter dbAdapter2;
private EditText editTextHem;
private Button button10;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_morfologia);
editTextHem = (EditText) findViewById(R.id.editText1_hemoglobina);
button10 = (Button) findViewById(R.id.button1_morfologia);
button10.setOnClickListener(l2);
}
private OnClickListener l2 = new OnClickListener() {
@Override
public void onClick(View v) {
if (v.getId() == R.id.button1_morfologia){
saveToDB();
}
}
};
public void saveToDB() {
String hemoglobina = editTextHem.getText().toString();
Log.d("text: ", hemoglobina);
dbAdapter2.open();
dbAdapter2.insertMorphologyData(hemoglobina);
dbAdapter2.close();
}}
logcat的:
11-04 10:49:37.734: D/text:(21197): 123
11-04 10:49:37.734: D/AndroidRuntime(21197): Shutting down VM
11-04 10:49:37.734: W/dalvikvm(21197): threadid=1: thread exiting with uncaught exception (group=0x416ced40)
11-04 10:49:37.736: E/AndroidRuntime(21197): FATAL EXCEPTION: main
11-04 10:49:37.736: E/AndroidRuntime(21197): Process: com.example.bmicalculator, PID: 21197
11-04 10:49:37.736: E/AndroidRuntime(21197): java.lang.NullPointerException
11-04 10:49:37.736: E/AndroidRuntime(21197): at com.example.bmicalculator.Morfologia.saveToDB(Morfologia.java:54)
11-04 10:49:37.736: E/AndroidRuntime(21197): at com.example.bmicalculator.Morfologia$1.onClick(Morfologia.java:42)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.view.View.performClick(View.java:4456)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.view.View$PerformClick.run(View.java:18465)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.os.Handler.handleCallback(Handler.java:733)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.os.Handler.dispatchMessage(Handler.java:95)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.os.Looper.loop(Looper.java:136)
11-04 10:49:37.736: E/AndroidRuntime(21197): at android.app.ActivityThread.main(ActivityThread.java:5086)
11-04 10:49:37.736: E/AndroidRuntime(21197): at java.lang.reflect.Method.invokeNative(Native Method)
11-04 10:49:37.736: E/AndroidRuntime(21197): at java.lang.reflect.Method.invoke(Method.java:515)
11-04 10:49:37.736: E/AndroidRuntime(21197): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
11-04 10:49:37.736: E/AndroidRuntime(21197): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
11-04 10:49:37.736: E/AndroidRuntime(21197): at dalvik.system.NativeStart.main(Native Method)
使用saveToDB方法的类:
package com.example.bmicalculator.database;
public class DatabaseAdapter {
SQLiteDatabase database;
DatabaseOpenHelper dbHelper;
public DatabaseAdapter(Context context) {
dbHelper = new DatabaseOpenHelper(context);
}
public void open() {
database = dbHelper.getWritableDatabase();
}
public void close() {
database.close();
}
public void insertMorphologyData(String hemoglobina){
ContentValues values2 = new ContentValues();
values2.put("hemoglobina", hemoglobina);
database.insert("morfologia_krwi2", null, values2);
}
}
班级创建表:
public class DatabaseOpenHelper extends SQLiteOpenHelper {
public static final String dbName = "baza";
public static final String morphologyTableName = "morfologia_krwi2";
public static final String createTableMorphology = "CREATE TABLE " + morphologyTableName +
"(id INTEGER PRIMARY KEY AUTOINCREMENT," +
"hemoglobina TEXT);";
public DatabaseOpenHelper(Context context) {
super(context, dbName, null, 1);
// TODO Auto-generated constructor stub
}
@Override
public void onCreate(SQLiteDatabase database) {
// TODO Auto-generated method stub
database.execSQL(createTableMorphology);
}
@Override
public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
}
}
答案 0 :(得分:2)
您只声明了dbadapter2
但从未初始化它。
使用
dbadapter2 = new DatabaseAdapter(this);
初始化它,this
例如您Activity
的{{1}}。