当我尝试删除Admin类中的一个用户名/密码/电子邮件列表视图行时,我的DBAdapter类中的deletePlayer会出现NullPointerException。我知道NPE来自我的删除方法,因为当我删除db.delete,打开和关闭我的toast在onclick上工作,但是当我实现它们时,我得到NPE。我很确定这是我的删除方法,但我已经尝试搜索我可以使用的正确删除方法但是我找不到涉及删除数据库中用户的USERNAME,PASSWORD和EMAIL列的内容。
更新:我包含了DBAdapter(上下文)并将我的光标包含在我的数据库中的deletePlayer方法中,但现在我得到了一个代码1语法错误,如LogCat所示
logcat的
04-20 14:56:51.289: E/Trace(20896): error opening trace file: No such file or directory (2)
04-20 14:56:51.664: D/dalvikvm(20896): GC_FOR_ALLOC freed 78K, 11% free 8200K/9119K, paused 34ms, total 34ms
04-20 14:56:52.344: D/libEGL(20896): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-20 14:56:52.797: D/libEGL(20896): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-20 14:56:52.805: D/libEGL(20896): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-20 14:56:53.360: D/OpenGLRenderer(20896): Enabling debug mode 0
04-20 14:57:00.000: D/dalvikvm(20896): GC_CONCURRENT freed 66K, 3% free 9899K/10147K, paused 14ms+2ms, total 86ms
04-20 14:59:07.102: E/Trace(21554): error opening trace file: No such file or directory (2)
04-20 14:59:07.164: D/dalvikvm(21554): GC_FOR_ALLOC freed 61K, 11% free 8200K/9119K, paused 29ms, total 29ms
04-20 14:59:07.696: D/libEGL(21554): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-20 14:59:07.891: D/libEGL(21554): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-20 14:59:07.899: D/libEGL(21554): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-20 14:59:08.149: D/OpenGLRenderer(21554): Enabling debug mode 0
04-20 14:59:10.672: E/SQLiteLog(21554): (1) near ".": syntax error
04-20 14:59:10.672: D/AndroidRuntime(21554): Shutting down VM
04-20 14:59:10.672: W/dalvikvm(21554): threadid=1: thread exiting with uncaught exception (group=0x419742a0)
04-20 14:59:10.891: E/AndroidRuntime(21554): FATAL EXCEPTION: main
04-20 14:59:10.891: E/AndroidRuntime(21554): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: DELETE FROM PLAYERS WHERE _id = android.database.sqlite.SQLiteCursor@4230c108
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1491)
04-20 14:59:10.891: E/AndroidRuntime(21554): at com.fullfrontalgames.numberfighter.DBAdapter.deletePlayer(DBAdapter.java:106)
04-20 14:59:10.891: E/AndroidRuntime(21554): at com.fullfrontalgames.numberfighter.Admin$MyListAdapter$1.onClick(Admin.java:66)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.view.View.performClick(View.java:4106)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.view.View$PerformClick.run(View.java:17052)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.os.Handler.handleCallback(Handler.java:615)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.os.Looper.loop(Looper.java:137)
04-20 14:59:10.891: E/AndroidRuntime(21554): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-20 14:59:10.891: E/AndroidRuntime(21554): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 14:59:10.891: E/AndroidRuntime(21554): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 14:59:10.891: E/AndroidRuntime(21554): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-20 14:59:10.891: E/AndroidRuntime(21554): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-20 14:59:10.891: E/AndroidRuntime(21554): at dalvik.system.NativeStart.main(Native Method)
04-20 14:59:10.961: D/dalvikvm(21554): GC_CONCURRENT freed 88K, 3% free 9886K/10147K, paused 13ms+5ms, total 75ms
04-20 14:59:13.172: I/Process(21554): Sending signal. PID: 21554 SIG: 9
04-20 15:03:13.141: E/Trace(22194): error opening trace file: No such file or directory (2)
04-20 15:03:13.399: D/dalvikvm(22194): GC_FOR_ALLOC freed 79K, 11% free 8200K/9119K, paused 57ms, total 58ms
04-20 15:03:13.844: D/libEGL(22194): loaded /vendor/lib/egl/libEGL_POWERVR_SGX540_120.so
04-20 15:03:13.883: D/libEGL(22194): loaded /vendor/lib/egl/libGLESv1_CM_POWERVR_SGX540_120.so
04-20 15:03:13.891: D/libEGL(22194): loaded /vendor/lib/egl/libGLESv2_POWERVR_SGX540_120.so
04-20 15:03:14.055: D/OpenGLRenderer(22194): Enabling debug mode 0
04-20 15:03:17.352: E/SQLiteLog(22194): (1) near ".": syntax error
04-20 15:03:17.375: D/AndroidRuntime(22194): Shutting down VM
04-20 15:03:17.375: W/dalvikvm(22194): threadid=1: thread exiting with uncaught exception (group=0x419742a0)
04-20 15:03:17.414: E/AndroidRuntime(22194): FATAL EXCEPTION: main
04-20 15:03:17.414: E/AndroidRuntime(22194): android.database.sqlite.SQLiteException: near ".": syntax error (code 1): , while compiling: DELETE FROM PLAYERS WHERE _id = android.database.sqlite.SQLiteCursor@4230c2a8
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:882)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:493)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteStatement.<init>(SQLiteStatement.java:31)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.database.sqlite.SQLiteDatabase.delete(SQLiteDatabase.java:1491)
04-20 15:03:17.414: E/AndroidRuntime(22194): at com.fullfrontalgames.numberfighter.DBAdapter.deletePlayer(DBAdapter.java:106)
04-20 15:03:17.414: E/AndroidRuntime(22194): at com.fullfrontalgames.numberfighter.Admin$MyListAdapter$1.onClick(Admin.java:66)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.view.View.performClick(View.java:4106)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.view.View$PerformClick.run(View.java:17052)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.os.Handler.handleCallback(Handler.java:615)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.os.Handler.dispatchMessage(Handler.java:92)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.os.Looper.loop(Looper.java:137)
04-20 15:03:17.414: E/AndroidRuntime(22194): at android.app.ActivityThread.main(ActivityThread.java:5059)
04-20 15:03:17.414: E/AndroidRuntime(22194): at java.lang.reflect.Method.invokeNative(Native Method)
04-20 15:03:17.414: E/AndroidRuntime(22194): at java.lang.reflect.Method.invoke(Method.java:511)
04-20 15:03:17.414: E/AndroidRuntime(22194): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:792)
04-20 15:03:17.414: E/AndroidRuntime(22194): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:555)
04-20 15:03:17.414: E/AndroidRuntime(22194): at dalvik.system.NativeStart.main(Native Method)
DBAdapter类
package com.fullfrontalgames.numberfighter;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBAdapter {
static final String KEY_ROWID = "_id";
static final String KEY_USERNAME = "USERNAME";
static final String KEY_PASSWORD = "PASSWORD";
static final String KEY_EMAIL = "EMAIL";
static final String KEY_NUMBERINPUT = "NUMBERINPUT";
static final String KEY_SCORE = "SCORE";
static final String KEY_FRIENDS = "FRIENDS";
static final String TAG = "DBAdapter";
static final String DATABASE_NAME = "NFDatabase";
static final String DATABASE_TABLE1 = "PLAYERS";
static final String DATABASE_TABLE2 = "FRIENDSLIST";
static final int DATABASE_VERSION = 2;
static final String DATABASE_CREATE_TABLE1 = "create table PLAYERS ( _id integer primary key autoincrement, "
+ "USERNAME text not null,PASSWORD text not null,EMAIL text,NUMBERINPUT text,SCORE text);";
static final String DATBASE_CREATE_TABLE2 = "create table FRIENDSLIST (_id integer primary key autoincrement,"
+ "FRIENDS text not null,USERNAME text not null,NUMBERINPUT text,SCORE text);";
final Context context;
DatabaseHelper DBHelper;
static SQLiteDatabase db;
public DBAdapter(Context ctx) {
this.context = ctx;
DBHelper = new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper {
public DatabaseHelper(Context context) {
// TODO Auto-generated constructor stub
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
// TODO Auto-generated method stub
try {
db.execSQL(DATABASE_CREATE_TABLE1);
db.execSQL(DATBASE_CREATE_TABLE2);
} catch (SQLException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
// TODO Auto-generated method stub
Log.w(TAG, "Upgrading database from version " + oldVersion + " to " + newVersion
+ ", which will destroy all old data");
db.execSQL("DROP TABLE IF EXISTS PLAYERS");
db.execSQL("DROP TABLE IF EXISTS FRIENDSLIST");
onCreate(db);
}
}
public DBAdapter open() throws SQLiteException {
db = DBHelper.getWritableDatabase();
return this;
}
public void close() {
DBHelper.close();
}
// PLAYERS TABLE CRUD
public void insertPlayer(String Username, String Password, String Email) {
ContentValues initialValues = new ContentValues();
initialValues.put("USERNAME", Username);
initialValues.put("PASSWORD", Password);
initialValues.put("EMAIL", Email);
db.insert("PLAYERS", null, initialValues);
}
public boolean deletePlayer(Cursor playeraccounts) {
return db.delete("PLAYERS", KEY_ROWID + " = " + playeraccounts, null) > 0;
}
public Cursor getAllPlayers() {
return db.query(false, "Players", new String[] {
"_id", "USERNAME", "PASSWORD", "EMAIL"
}, null, null, null, null, null, null);
}
public String getData() {
String[] columns = new String[] {
"_id", "USERNAME", "PASSWORD"
};
Cursor mCursor = db.query("PLAYERS", columns, null, null, null, null, null);
String result = "";
int iRow = mCursor.getColumnIndex(KEY_ROWID);
int iName = mCursor.getColumnIndex(KEY_USERNAME);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
result = mCursor.getString(iRow) + " " + mCursor.getString(iName) + "/n";
}
return result;
}
public Cursor getPlayer(long rowid) throws SQLException {
Cursor mCursor = db.query(true, "PLAYERS", new String[] {
"_id", "USERNAME"
}, KEY_ROWID + " = " + "_id", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public String getUsername(String username) {
// TODO Auto-generated method stub
String[] columns = new String[] {
"_id", "USERNAME"
};
Cursor mCursor = db.query("PLAYERS", columns, null, null, null, null, null);
String result = "";
int iRow = mCursor.getColumnIndex(KEY_ROWID);
int iName = mCursor.getColumnIndex(KEY_USERNAME);
for (mCursor.moveToFirst(); !mCursor.isAfterLast(); mCursor.moveToNext()) {
result = mCursor.getString(iRow) + " " + mCursor.getString(iName) + "/n";
}
return result;
}
public String getSinlgeEntry(String Username) {
Cursor cursor = db.query("PLAYERS", null, " USERNAME=?", new String[] {
Username
}, null, null, null);
if (cursor.getCount() < 1) // UserName Not Exist
{
cursor.close();
return "NOT EXIST";
}
cursor.moveToFirst();
String password = cursor.getString(cursor.getColumnIndex("PASSWORD"));
cursor.close();
return password;
}
public boolean updatePlayer(long _id, String Username, String Password, String Email) {
ContentValues args = new ContentValues();
args.put("USERNAME", Username);
args.put("PASSWORD", Password);
args.put("EMAIL", Email);
return db.update("PLAYERS", args, KEY_ROWID + " = " + _id, null) > 0;
}
// FRIENDS TABLE CRUD
public void insertFriend(String Friend) {
ContentValues initialValues = new ContentValues();
initialValues.put("FRIENDS", Friend);
db.insert("FRIENDSLIST", null, null);
}
public boolean deleteFriend() {
return db.delete("FRIENDSLIST", KEY_ROWID + " = " + "_id", null) > 0;
}
public Cursor getAllFriends() {
return db.query(null, new String[] {
"_id", "FRIENDS"
}, null, null, null, null, null, null);
}
public Cursor getFriend(long rowid) throws SQLException {
Cursor mCursor = db.query(true, "FRIENDSLIST", new String[] {
"_id", "FRIENDS"
}, KEY_ROWID + " = " + "_id", null, null, null, null, null);
if (mCursor != null) {
mCursor.moveToFirst();
}
return mCursor;
}
public boolean updateFriend(long _id, String Friends) {
ContentValues args = new ContentValues();
args.put("FRIENDS", Friends);
return db.update("FRIENDSLIST", args, KEY_ROWID + " = " + _id, null) > 0;
}
}
管理员班级
package com.fullfrontalgames.numberfighter;
import android.app.ListActivity;
import android.content.Context;
import android.database.Cursor;
import android.os.Bundle;
import android.support.v4.widget.ResourceCursorAdapter;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
public class Admin extends ListActivity {
DBAdapter db;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
DBAdapter db = new DBAdapter(this);
db.open();
ListView UsernameList = (ListView)findViewById(android.R.id.list);
Button deleteplayer = (Button)findViewById(R.id.PlayerDelete);
Cursor playeraccounts = db.getAllPlayers();
startManagingCursor(playeraccounts);
setListAdapter(new MyListAdapter(this, R.layout.admin, playeraccounts));
}
private class MyListAdapter extends ResourceCursorAdapter {
DBAdapter db;
public MyListAdapter(Context context, int layout, Cursor cursor) {
super(context, R.layout.admin, cursor);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, final Context context, final Cursor playeraccounts) {
final TextView PlayerName = (TextView)findViewById(R.id.AdminUsername);
final TextView PlayerPass = (TextView)findViewById(R.id.AdminPassword);
final TextView PlayerEmail = (TextView)findViewById(R.id.AdminEmail);
PlayerName.setText(playeraccounts.getString(playeraccounts.getColumnIndex("USERNAME")));
PlayerPass.setText(playeraccounts.getString(playeraccounts.getColumnIndex("PASSWORD")));
PlayerEmail.setText(playeraccounts.getString(playeraccounts.getColumnIndex("EMAIL")));
Button deleteplayer = (Button)findViewById(R.id.PlayerDelete);
deleteplayer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
DBAdapter db = new DBAdapter(context);
db.open();
db.deletePlayer(playeraccounts);
Toast.makeText(getBaseContext(), "Player Deleted", Toast.LENGTH_SHORT).show();
MyListAdapter.this.notifyDataSetChanged();
db.close();
}
});
}
}
}
答案 0 :(得分:1)
你的数据库DBAdapter
是空的,
当您尝试对其执行操作时
即
db.open();
会抛出NullPointerException
因此,尝试在适配器的构造函数中实例化db
db=new DBAdapter(context); when you are using them in separate classess.
或将db
声明为字段,然后使用它。删除适配器中的新对象。
答案 1 :(得分:0)
您有两个DBAdapter db
个对象。 onCreate()
中的那个被初始化,而MyListAdapter class
中的那个被保持为空。
您应该只维护一个db
对象,或者初始化它们。
这应该有用。
public class Admin extends ListActivity {
DBAdapter db;
@SuppressWarnings("deprecation")
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.admin);
db = new DBAdapter(this);
db.open();
ListView UsernameList = (ListView)findViewById(android.R.id.list);
Button deleteplayer = (Button)findViewById(R.id.PlayerDelete);
Cursor playeraccounts = db.getAllPlayers();
startManagingCursor(playeraccounts);
setListAdapter(new MyListAdapter(this, R.layout.admin, playeraccounts));
}
private class MyListAdapter extends ResourceCursorAdapter {
public MyListAdapter(Context context, int layout, Cursor cursor) {
super(context, R.layout.admin, cursor);
// TODO Auto-generated constructor stub
}
@Override
public void bindView(View view, Context context, Cursor playeraccounts) {
final TextView PlayerName = (TextView)findViewById(R.id.AdminUsername);
final TextView PlayerPass = (TextView)findViewById(R.id.AdminPassword);
final TextView PlayerEmail = (TextView)findViewById(R.id.AdminEmail);
PlayerName.setText(playeraccounts.getString(playeraccounts.getColumnIndex("USERNAME")));
PlayerPass.setText(playeraccounts.getString(playeraccounts.getColumnIndex("PASSWORD")));
PlayerEmail.setText(playeraccounts.getString(playeraccounts.getColumnIndex("EMAIL")));
Button deleteplayer = (Button)findViewById(R.id.PlayerDelete);
deleteplayer.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
// TODO Auto-generated method stub
db.open();
db.deletePlayer(0);
Toast.makeText(getBaseContext(), "Player Deleted", Toast.LENGTH_SHORT).show();
MyListAdapter.this.notifyDataSetChanged();
db.close();
}
});
}
}
}