尝试在空对象引用
上调用虚方法int android.database.sqlite.SQLiteDatabase.delete(java.lang.String, java.lang.String, java.lang.String[])
由于我的代码很长,与手头问题无关的部分已被删除(它们工作正常,与数据库无关)
这是我的代码:
protected static final String ACTIVITY_NAME = "ChatWindow";
ListView myDisplay;
EditText myChat;
Button mySend;
ArrayList<String> myList;
ChatAdapter messageAdapter;
public static Cursor cursor;
SQLiteDatabase db;
Boolean isTablet;
FrameLayout fLayout;
ChatDatabaseHelper cdh;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_chat_window);
//declarations
myDisplay = (ListView) findViewById(R.id.display);
myChat = (EditText) findViewById(R.id.chat);
mySend = (Button) findViewById(R.id.sndBtn);
myList = new ArrayList<>();
//in this case, “this” is the ChatWindow, which is-A Context object
messageAdapter =new ChatAdapter( this );
myDisplay.setAdapter (messageAdapter);
cdh = new ChatDatabaseHelper(this);
db = cdh.getWritableDatabase();
cursor = db.rawQuery("SELECT * from ChatDatabaseHelper", null);
cursor.moveToFirst();
while(!cursor.isAfterLast() ){
myList.add(cursor.getString(cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE)));
Log.i(ACTIVITY_NAME, "SQL MESASAGE:" + cursor.getString(
cursor.getColumnIndex(ChatDatabaseHelper.KEY_MESSAGE)));
Log.i(ACTIVITY_NAME, "Cursor's column count =" + cursor.getColumnCount() );
cursor.moveToNext();
//send button actions
mySend.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String message;
message = myChat.getText().toString();
myList.add(message);
messageAdapter.notifyDataSetChanged(); //this restarts the process of getCount()/getView()
myChat.setText("");
ContentValues cValues = new ContentValues();
cValues.put(ChatDatabaseHelper.KEY_MESSAGE, message);
db.insert(TABLE_NAME, "NullColumnName", cValues);
}
});
//This is where I need help
public void deleteEntry (String id) {
Log.i("deleteRow", id);
//The log prints just fine so this method is being run and I have the correct value for id
db.delete(ChatDatabaseHelper.TABLE_NAME, ChatDatabaseHelper.KEY_ID + "=" + id, null);
}
//You can ignore anything below
private class ChatAdapter extends ArrayAdapter<String> {
public ChatAdapter(Context ctx) {
super(ctx, 0);
}
@Override
public int getCount() {
return myList.size();
}
@Override
public String getItem(int position) {
return myList.get(position);
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = ChatWindow.this.getLayoutInflater();
View result = null ;
if(position%2 == 0)
result = inflater.inflate(R.layout.chat_row_incoming, null);
else
result = inflater.inflate(R.layout.chat_row_outgoing, null);
TextView message = (TextView)result.findViewById(R.id.message_text);
message.setText( getItem(position) ); // get the string at position
return result;
}
public long getId(int position) {
return Long.parseLong(myList.get(position));
}
public long getItemId(int position) {
cursor.moveToPosition(position);
return cursor.getInt(cursor.getColumnIndex(ChatDatabaseHelper.KEY_ID));
}
}
protected void onDestroy() {
super.onDestroy();
cursor.close();
db.close();
}
以下是数据库活动的代码:
public class ChatDatabaseHelper extends SQLiteOpenHelper {
private static int VERSION_NUM = 1;
private static String DATABASE_NAME = "Messages.db";
final static String TABLE_NAME = "ChatDatabaseHelper";
final static String KEY_ID = "id";
final static String KEY_MESSAGE = "MESSAGE";
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + "( "
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_MESSAGE + " text "
+ " ); ";
public ChatDatabaseHelper(Context ctx) {
super(ctx, DATABASE_NAME, null, VERSION_NUM);
}
@Override
public void onCreate(SQLiteDatabase db) {
db.execSQL(CREATE_TABLE);
Log.i("ChatDatabaseHelper", "Calling onCreate");
}
public void onUpgrade(SQLiteDatabase db) {
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME + "'");
onCreate(db);
}
public void onUpgrade(SQLiteDatabase db, int oldVer, int newVer) {
Log.i("ChatDatabaseHelper", "Calling onUpgrade, oldVersion=" + oldVer + " newVersion=" + newVer);
db.execSQL("DROP TABLE IF EXISTS '" + TABLE_NAME + "'");
onCreate(db);
}
}
这是我的堆栈跟踪:
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.himanshu.androidlabs, PID: 22613
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.database.sqlite.SQLiteDatabase.delete(java.lang.String, java.lang.String, java.lang.String[])' on a null object reference
at com.example.himanshu.androidlabs.ChatWindow.deleteEntry(ChatWindow.java:159)
at com.example.himanshu.androidlabs.MessageFragment$1.onClick(MessageFragment.java:55)
at android.view.View.performClick(View.java:6256)
at android.view.View$PerformClick.run(View.java:24701)
at android.os.Handler.handleCallback(Handler.java:789)
at android.os.Handler.dispatchMessage(Handler.java:98)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6541)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:767)