我正在尝试通过简单的游标适配器将我的数据库填充到listview,意图是新活动。不知道为什么会崩溃?
这是我的数据库助手类:
public class DBhelper extends SQLiteOpenHelper {
public static final String DATABASE_NAME = "Mydatabase.db";
public static final String TABLE_NAME = "contacts";
public static final String _ID = "_ID";
public static final String COL_2 = "NAME";
public static final String COL_3 = "PHONE";
public static final String COL_4 = "EMAIL";
public static final String COL_5 = "HQ";
public static final String COL_6 = "ADDRESS";
public DBhelper(Context context) {
super(context, DATABASE_NAME, null, 1);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL("create table contacts"+ "(_id integer primary key autoincrement, name text, phone text, email text, hq text, address text)");
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("DROP TABLE IF EXISTS contacts");
onCreate(db);
}
public boolean insertContact (String name, String phone, String email, String hq, String address){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_2,name);
contentValues.put(COL_3,phone);
contentValues.put(COL_4,email);
contentValues.put(COL_5,hq);
contentValues.put(COL_6,address);
db.insert(TABLE_NAME,null,contentValues);
return true;
}
public Cursor getAllDAta(){
Cursor cursor;
String[] columns = {DBhelper._ID, DBhelper.COL_2, DBhelper.COL_3, DBhelper.COL_4, DBhelper.COL_5, DBhelper.COL_6};
SQLiteDatabase db = this.getWritableDatabase();
cursor = db.query(DBhelper.TABLE_NAME,columns, null, null, null, null, null);
return cursor;
}
DBhelper dbHelper;
SQLiteDatabase database;
Context context;
public DBhelper open() throws SQLException {
dbHelper = new DBhelper(context);
database = dbHelper.getWritableDatabase();
return this;
}
}
MainActivity.java
public class MainActivity extends AppCompatActivity {
DBhelper mydb;
EditText name, phone, email, hq, address;
Button addData, viewData;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mydb = new DBhelper(this);
name = (EditText) findViewById(R.id.editName);
phone = (EditText) findViewById(R.id.editPhone);
email = (EditText) findViewById(R.id.editEmail);
hq = (EditText) findViewById(R.id.editHQ);
address = (EditText) findViewById(R.id.editAddress);
addData = (Button) findViewById(R.id.addData);
viewData = (Button) findViewById(R.id.viewData);
}
public void addClick(View view){
String nm, ph, em, h, as;
nm = name.getText().toString();
ph = phone.getText().toString();
em = email.getText().toString();
h = hq.getText().toString();
as = address.getText().toString();
boolean checkk = mydb.insertContact(nm,ph,em,h,as);
if (checkk = true) {
Toast.makeText(MainActivity.this, "Data Added", Toast.LENGTH_LONG).show();
}else {
Toast.makeText(MainActivity.this,"Data Not Added",Toast.LENGTH_LONG).show();
}
}
public void showClick(View v1){
Intent intent = new Intent(MainActivity.this,Displayc.class);
startActivity(intent);
finish();
}
}
我的展示活动
public class Displayc extends AppCompatActivity {
ListView listView;
DBhelper mydb1;
SimpleCursorAdapter adapter;
final String[] from = new String[] {DBhelper._ID, DBhelper.COL_2, DBhelper.COL_3, DBhelper.COL_4, DBhelper.COL_5, DBhelper.COL_6};
final int[] to = new int[] {R.id.idv, R.id.namev, R.id.phonev, R.id.emailv, R.id.hqv, R.id.addressv };
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_displayc);
listView = (ListView) findViewById(R.id.list_sq);
mydb1 = new DBhelper(this.getBaseContext());
mydb1.open();
Cursor c = mydb1.getAllDAta();
adapter = new SimpleCursorAdapter(getBaseContext(),R.layout.template,c,from,to,0);
listView.setAdapter(adapter);
}
}
错误讯息:
[java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.bibulkumar.sqliteapp/com.example.bibulkumar.sqliteapp.Displayc}: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
[Caused by: java.lang.NullPointerException: Attempt to invoke virtual method 'android.database.sqlite.SQLiteDatabase android.content.Context.openOrCreateDatabase(java.lang.String, int, android.database.sqlite.SQLiteDatabase$CursorFactory, android.database.DatabaseErrorHandler)' on a null object reference
答案 0 :(得分:0)
Context context; public DBhelper open() throws SQLException { dbHelper = new DBhelper(context); database = dbHelper.getWritableDatabase(); return this;
您在此处使用的context
未初始化,因此null
。因此,当你打电话给getWritableDatabase()
时,就是NPE。
删除open()
以及对它的任何调用 - 您实际上并不需要它。