public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
super.onCreateContextMenu(menu, v, menuInfo);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.context_menu, menu);
}
public boolean onContextItemSelected(MenuItem item) {
//AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
//String[] names = getResources().getStringArray(R.array.names);
switch(item.getItemId())
{
case R.id.viewData:
{ Intent r=new Intent(Database.this,MainMenu.class);
startActivity(r);
return true;
}
case R.id.DelData:
{
AdapterView.AdapterContextMenuInfo info1=
(AdapterView.AdapterContextMenuInfo)item.getMenuInfo();
delete(info1.id);
return true;
}
}
return(super.onOptionsItemSelected(item));
}
private void delete(final long rowId) {
if (rowId>0) {
new AlertDialog.Builder(this)
.setTitle(R.string.delete_title)
.setPositiveButton(R.string.ok,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
processDelete(rowId);
}
})
.setNegativeButton(R.string.cancel,
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// ignore, just dismiss
}
})
.show();
}
}
private void processDelete(long rowId) {
String[] args={String.valueOf(rowId)};
//DatabaseHelper dbh = new DatabaseHelper(this);
dbh.getWritableDatabase().delete("mygrades", "_ID=?", args);
c.requery();
}
我正在从表名为mygrades的数据库中删除行。有一个contextMenu,我有两个选项查看和删除。当我按下删除程序停止。错误日志说我遇到了processDelete(long rowId)的问题。我的论点是否以正确的方式传递?我将在下面发布错误日志: -
04-02 03:38:29.450: W/dalvikvm(541): threadid=1: thread exiting with uncaught exception (group=0x409c01f8)
04-02 03:38:29.489: E/AndroidRuntime(541): FATAL EXCEPTION: main
04-02 03:38:29.489: E/AndroidRuntime(541): java.lang.NullPointerException
04-02 03:38:29.489: E/AndroidRuntime(541): at nidhin.survey.Database.processDelete(Database.java:139)
04-02 03:38:29.489: E/AndroidRuntime(541): at nidhin.survey.Database.access$0(Database.java:135)
04-02 03:38:29.489: E/AndroidRuntime(541): at nidhin.survey.Database$1.onClick(Database.java:121)
04-02 03:38:29.489: E/AndroidRuntime(541): at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:166)
04-02 03:38:29.489: E/AndroidRuntime(541): at android.os.Handler.dispatchMessage(Handler.java:99)
04-02 03:38:29.489: E/AndroidRuntime(541): at android.os.Looper.loop(Looper.java:137)
04-02 03:38:29.489: E/AndroidRuntime(541): at android.app.ActivityThread.main(ActivityThread.java:4424)
04-02 03:38:29.489: E/AndroidRuntime(541): at java.lang.reflect.Method.invokeNative(Native Method)
04-02 03:38:29.489: E/AndroidRuntime(541): at java.lang.reflect.Method.invoke(Method.java:511)
04-02 03:38:29.489: E/AndroidRuntime(541): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:784)
04-02 03:38:29.489: E/AndroidRuntime(541): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:551)
04-02 03:38:29.489: E/AndroidRuntime(541): at dalvik.system.NativeStart.main(Native Method)
04-02 03:38:32.388: I/Process(541): Sending signal. PID: 541 SIG: 9
04-02 03:39:09.788: D/dalvikvm(586): GC_FOR_ALLOC freed 66K, 4% free 9065K/9347K, paused 82ms
04-02 03:39:09.788: I/dalvikvm-heap(586): Grow heap (frag case) to 9.452MB for 556016-byte allocation
04-02 03:39:09.930: D/dalvikvm(586): GC_CONCURRENT freed <1K, 4% free 9607K/9927K, paused 5ms+5ms
04-02 03:39:10.259: D/gralloc_goldfish(586): Emulator without GPU emulation detected.
答案 0 :(得分:2)
我有一个很好的数据库问题教程。试试这个DatabaseHelper代码,它对你有用。
public class DatabaseHelper
{
Context context;
private SQLiteDatabase db;
private final String DB_NAME = "database_name";
private final int DB_VERSION = 1;
private final String TABLE_NAME = "database_table";
private final String TABLE_ROW_ID = "id";
private final String TABLE_ROW_ONE = "table_row_one";
private final String TABLE_ROW_TWO = "table_row_two";
public DatabaseHelper(Context context)
{
this.context = context;
CustomSQLiteOpenHelper helper = new CustomSQLiteOpenHelper(context);
this.db = helper.getWritableDatabase();
}
/**********************************************************************
* ADDING A ROW TO THE DATABASE TABLE
*
* This is an example of how to add a row to a database table
* using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
* @param rowStringOne the value for the row's first column
* @param rowStringTwo the value for the row's second column
*/
public void addRow(String rowStringOne, String rowStringTwo)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
try{db.insert(TABLE_NAME, null, values);}
catch(Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* DELETING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to delete a row from a database table
* using this class. In most cases, this method probably does
* not need to be rewritten.
*
* @param rowID the SQLite database identifier for the row to delete.
*/
public void deleteRow(long rowID)
{
// ask the database manager to delete the row of given id
try {db.delete(TABLE_NAME, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* UPDATING A ROW IN THE DATABASE TABLE
*
* This is an example of how to update a row in the database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the SQLite database identifier for the row to update.
* @param rowStringOne the new value for the row's first column
* @param rowStringTwo the new value for the row's second column
*/
public void updateRow(long rowID, String rowStringOne, String rowStringTwo)
{
ContentValues values = new ContentValues();
values.put(TABLE_ROW_ONE, rowStringOne);
values.put(TABLE_ROW_TWO, rowStringTwo);
// ask the database object to update the database row of given rowID
try {db.update(TABLE_NAME, values, TABLE_ROW_ID + "=" + rowID, null);}
catch (Exception e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
}
/**********************************************************************
* RETRIEVING A ROW FROM THE DATABASE TABLE
*
* This is an example of how to retrieve a row from a database table
* using this class. You should edit this method to suit your needs.
*
* @param rowID the id of the row to retrieve
* @return an array containing the data from the row
*/
public ArrayList<Object> getRowAsArray(long rowID)
{
// create an array list to store data from the database row.
// I would recommend creating a JavaBean compliant object
// to store this data instead. That way you can ensure
// data types are correct.
ArrayList<Object> rowArray = new ArrayList<Object>();
Cursor cursor;
try
{
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
cursor = db.query
(
TABLE_NAME,
new String[] { TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO },
TABLE_ROW_ID + "=" + rowID,
null, null, null, null, null
);
// move the pointer to position zero in the cursor.
cursor.moveToFirst();
// if there is data available after the cursor's pointer, add
// it to the ArrayList that will be returned by the method.
if (!cursor.isAfterLast())
{
do
{
rowArray.add(cursor.getLong(0));
rowArray.add(cursor.getString(1));
rowArray.add(cursor.getString(2));
}
while (cursor.moveToNext());
}
// let java know that you are through with the cursor.
cursor.close();
}
catch (SQLException e)
{
Log.e("DB ERROR", e.toString());
e.printStackTrace();
}
// return the ArrayList containing the given row from the database.
return rowArray;
}
/**********************************************************************
* RETRIEVING ALL ROWS FROM THE DATABASE TABLE
*
* This is an example of how to retrieve all data from a database
* table using this class. You should edit this method to suit your
* needs.
*
* the key is automatically assigned by the database
*/
public ArrayList<ArrayList<Object>> getAllRowsAsArrays()
{
// create an ArrayList that will hold all of the data collected from
// the database.
ArrayList<ArrayList<Object>> dataArrays = new ArrayList<ArrayList<Object>>();
// this is a database call that creates a "cursor" object.
// the cursor object store the information collected from the
// database and is used to iterate through the data.
Cursor cursor;
try
{
// ask the database object to create the cursor.
cursor = db.query(
TABLE_NAME,
new String[]{TABLE_ROW_ID, TABLE_ROW_ONE, TABLE_ROW_TWO},
null, null, null, null, null
);
// move the cursor's pointer to position zero.
cursor.moveToFirst();
// if there is data after the current cursor position, add it
// to the ArrayList.
if (!cursor.isAfterLast())
{
do
{
ArrayList<Object> dataList = new ArrayList<Object>();
dataList.add(cursor.getLong(0));
dataList.add(cursor.getString(1));
dataList.add(cursor.getString(2));
dataArrays.add(dataList);
}
// move the cursor's pointer up one position.
while (cursor.moveToNext());
}
}
catch (SQLException e)
{
Log.e("DB Error", e.toString());
e.printStackTrace();
}
// return the ArrayList that holds the data collected from
// the database.
return dataArrays;
}
/**********************************************************************
* THIS IS THE BEGINNING OF THE INTERNAL SQLiteOpenHelper SUBCLASS.
*
* I MADE THIS CLASS INTERNAL SO I CAN COPY A SINGLE FILE TO NEW APPS
* AND MODIFYING IT - ACHIEVING DATABASE FUNCTIONALITY. ALSO, THIS WAY
* I DO NOT HAVE TO SHARE CONSTANTS BETWEEN TWO FILES AND CAN
* INSTEAD MAKE THEM PRIVATE AND/OR NON-STATIC. HOWEVER, I THINK THE
* INDUSTRY STANDARD IS TO KEEP THIS CLASS IN A SEPARATE FILE.
*********************************************************************/
/**
* This class is designed to check if there is a database that currently
* exists for the given program. If the database does not exist, it creates
* one. After the class ensures that the database exists, this class
* will open the database for use. Most of this functionality will be
* handled by the SQLiteOpenHelper parent class. The purpose of extending
* this class is to tell the class how to create (or update) the database.
*
* @author Randall Mitchell
*
*/
private class CustomSQLiteOpenHelper extends SQLiteOpenHelper
{
public CustomSQLiteOpenHelper(Context context)
{
super(context, DB_NAME, null, DB_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db)
{
// This string is used to create the database. It should
// be changed to suit your needs.
String newTableQueryString = "create table " +
TABLE_NAME +
" (" +
TABLE_ROW_ID + " integer primary key autoincrement not null," +
TABLE_ROW_ONE + " text," +
TABLE_ROW_TWO + " text" +
");";
// execute the query string to the database.
db.execSQL(newTableQueryString);
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion)
{
// NOTHING TO DO HERE. THIS IS THE ORIGINAL DATABASE VERSION.
// OTHERWISE, YOU WOULD SPECIFIY HOW TO UPGRADE THE DATABASE.
}
}
}
答案 1 :(得分:2)
你有一个空指针。我的猜测是: