我已经使用sqlHandler,DatabaseHandler和一个带有ContactListAdapter和contact.java的ListView建立了一个数据库。
现在我在尝试启动应用时遇到了Nullpointerexception。 Logcat说没有TABLE_CONTACTS,但DatabaseHandler应该在onCreate上设置它!
这里的错误:
05-27 14:46:51.316: E/SQLiteLog(17898): (1) no such table: TABLE_CONTACTS
05-27 14:46:51.336: E/AndroidRuntime(17898): FATAL EXCEPTION: main
05-27 14:46:51.336: E/AndroidRuntime(17898): Process: de.poetjan.powerconsumption, PID: 17898
05-27 14:46:51.336: E/AndroidRuntime(17898): java.lang.NullPointerException
05-27 14:46:51.336: E/AndroidRuntime(17898): at de.poetjan.powerconsumption.Zahlerstand.showList(Zahlerstand.java:230)
05-27 14:46:51.336: E/AndroidRuntime(17898): at de.poetjan.powerconsumption.Zahlerstand.onCreateView(Zahlerstand.java:67)
05-27 14:46:51.336: E/AndroidRuntime(17898): at android.support.v4.app.Fragment.performCreateView(Fragment.java:1500)
05-27 14:46:51.336: E/AndroidRuntime(17898): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:927)
05-27 14:46:51.336: E/AndroidRuntime(17898): at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1104)
这里是我的Fragment(head)中的代码,直到调用showList() - Method。
public class Zahlerstand extends Fragment {
int mYear3;
int mMonth3;
int mDay3;
EditText dateDisplay3;
EditText kwhDisplay1;
String neuformatjahr3;
String neuformatmonat3;
String neuformattag3;
TextView test;
String gesamt3o;
Button pickdate;
SqlHandler sqlHandler;
ListView lvCustomList;
EditText etName, etPhone;
Button btnsubmit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sqlHandler = new SqlHandler(getActivity());
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.zahlerstand, container, false);
kwhDisplay1 = (EditText) rootView.findViewById(R.id.kwhDisplay1);
dateDisplay3 = (EditText) rootView.findViewById(R.id.dateDisplay3);
pickdate = (Button) rootView.findViewById(R.id.pickDate);
lvCustomList = (ListView) rootView.findViewById(R.id.lv_custom_list);
btnsubmit = (Button) rootView.findViewById(R.id.btn_submit);
test = (TextView) rootView.findViewById(R.id.textViewverb);
showList();
registerForContextMenu(lvCustomList);
btnsubmit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (dateDisplay3.getText().toString().isEmpty() || kwhDisplay1.getText().toString().isEmpty()) {
Toast.makeText(getActivity(), R.string.beideeingeben, Toast.LENGTH_SHORT).show();
}
else {
String name = dateDisplay3.getText().toString();
String phoneNo = kwhDisplay1.getText().toString();
String query = "INSERT INTO TABLE_CONTACTS(id, name, phone_number) values ('"
+ name + "','" + phoneNo + "')";
sqlHandler.executeQuery(query);
showList();
此处发生错误的showList()
方法的第230行:
private void showList() {
ArrayList<Contact> contactList = new ArrayList<Contact>();
contactList.clear();
String query = "SELECT * FROM TABLE_CONTACTS ";
Cursor c1 = sqlHandler.selectQuery(query);
if (c1 != null && c1.getCount() != 0) {
if (c1.moveToFirst()) {
do {
Contact contactListItems = new Contact();
contactListItems.setID(c1
.getColumnIndex("slno"));
contactListItems.setName(c1.getString(c1
.getColumnIndex("name")));
contactListItems.setPhoneNumber(c1.getString(c1
.getColumnIndex("phone")));
contactList.add(contactListItems);
} while (c1.moveToNext());
}
}
c1.close();
ContactListAdapter contactListAdapter = new ContactListAdapter(
getActivity(), contactList);
lvCustomList.setAdapter(contactListAdapter);
}
为了确保问题不在这里,sqlHandler.java和DatabaseHandler.java
SQLHandler:
public class SqlHandler {
public static final String DATABASE_NAME = "MY_DATABASE";
Context context;
SQLiteDatabase sqlDatabase;
DatabaseHandler dbHelper;
public SqlHandler(Context context) {
dbHelper = new DatabaseHandler(context);
sqlDatabase = dbHelper.getWritableDatabase();
}
public void executeQuery(String query) {
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
sqlDatabase.execSQL(query);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
}
public Cursor selectQuery(String query) {
Cursor c1 = null;
try {
if (sqlDatabase.isOpen()) {
sqlDatabase.close();
}
sqlDatabase = dbHelper.getWritableDatabase();
c1 = sqlDatabase.rawQuery(query, null);
} catch (Exception e) {
System.out.println("DATABASE ERROR " + e);
}
return c1;
}
}
这里是DatabaseHandler.java(当然只有head,所有CRUD-Operations都在下面):
public class DatabaseHandler extends SQLiteOpenHelper {
private static final int DATABASE_VERSION = 1;
private static final String DATABASE_NAME = "contactsManager";
// Contacts table name
private static final String TABLE_CONTACTS = "contacts";
// Contacts Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_PH_NO = "phone_number";
public DatabaseHandler(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// Creating Tables
@Override
public void onCreate(SQLiteDatabase db) {
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
db.execSQL(CREATE_CONTACTS_TABLE);
}
// Upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// Drop older table if existed
db.execSQL("DROP TABLE IF EXISTS " + TABLE_CONTACTS);
// Create tables again
onCreate(db);
}
最后是Contact.java:
public class Contact {
//private variables
int _id;
String _name;
String _phone_number;
// Empty constructor
public Contact(){
}
// constructor
public Contact(int id, String name, String _phone_number){
this._id = id;
this._name = name;
this._phone_number = _phone_number;
}
// constructor
public Contact(String name, String _phone_number){
this._name = name;
this._phone_number = _phone_number;
}
// getting ID
public int getID(){
return this._id;
}
// setting id
public void setID(int id){
this._id = id;
}
// getting name
public String getName(){
return this._name;
}
// setting name
public void setName(String name){
this._name = name;
}
// getting phone number
public String getPhoneNumber(){
return this._phone_number;
}
// setting phone number
public void setPhoneNumber(String phone_number){
this._phone_number = phone_number;
}
}
最后但并非最不重要的是ContactListAdapter.java:
public class ContactListAdapter extends BaseAdapter {
Context context;
ArrayList<Contact> contactList;
public ContactListAdapter(Context context, ArrayList<Contact> list) {
this.context = context;
contactList = list;
}
@Override
public int getCount() {
return contactList.size();
}
@Override
public Object getItem(int position) {
return contactList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup arg2) {
Contact contactListItems = contactList.get(position);
if (convertView == null) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.contact_list_row, null);
}
TextView tvSlNo = (TextView) convertView.findViewById(R.id.tv_slno);
tvSlNo.setText(String.valueOf(contactListItems.getID()));
TextView tvName = (TextView) convertView.findViewById(R.id.tv_name);
tvName.setText(contactListItems.getName());
TextView tvPhone = (TextView) convertView.findViewById(R.id.tv_phone);
tvPhone.setText(contactListItems.getPhoneNumber());
return convertView;
}
}
答案 0 :(得分:0)
在showList()
的第230行,您输入TABLE CONTACTS
作为表的名称,而在DatabaseHandler类中,您定义TABLE_CONTACTS = "contacts"
并创建一个名为"contacts"
的表:
String CREATE_CONTACTS_TABLE = "CREATE TABLE " + TABLE_CONTACTS + "("
+ KEY_ID + " INTEGER PRIMARY KEY," + KEY_NAME + " TEXT,"
+ KEY_PH_NO + " TEXT" + ")";
因此,当您在query
中编写showList()
变量时,您需要确保让它访问正确的表:
String query = "SELECT * FROM TABLE_CONTACTS ";
应该是:
String TABLE_CONTACTS = "contacts"
String query = "SELECT * FROM " + TABLE_CONTACTS;
您似乎已将变量名称与表格名称混淆。