嘿伙计们我正在尝试更新特定列表项的列值。但是当我尝试选择列表项时,它会抛出NullPointerException。它在其他情况下工作正常,但在这里它不会。下面是我的代码和堆栈跟踪
我已经注释了错误发生的位置
public class Quick extends AppCompatActivity {
EditText addAmount;
ListView selectCategory;
Button OK,CANCEL;
DBHelper dbHelper=new DBHelper(this);
AddListItems addListItems=new AddListItems();
String[] columns;
SimpleCursorAdapter cursorAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setTitle("Add Instant Expenses");
setContentView(R.layout.quick);
addAmount = (EditText)findViewById(R.id.howmuchspend);
selectCategory = (ListView)findViewById(R.id.listView);
OK = (Button)findViewById(R.id.buttonAddQuick);
CANCEL = (Button)findViewById(R.id.buttonCancelQuick);
displayList();
selectCategory.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {
add(id); /* error here */
Toast.makeText(getApplicationContext(), "Item with id " + id + " selected", Toast.LENGTH_LONG).show();
return true;
}
});
CANCEL.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Quick.this, MainActivity.class);
startActivity(intent);
}
});
OK.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(Quick.this, Quick.class);
startActivity(intent);
}
});
}
public void add(long itemId){
final int finAmount=Integer.parseInt(addAmount.getText().toString());
addListItems.addTotal(finAmount, itemId);
}
@TargetApi(11)
private void displayList() {
Cursor cursor;
cursor = dbHelper.getListItems();
if(cursor==null){
Intent intent=new Intent(Quick.this,AddListItems.class);
startActivity(intent);
}
else {
columns = new String[]{DBHelper.KEY_CATEGORY, DBHelper.KEY_AMOUNT};
int[] to = new int[]{R.id.category, R.id.amount};
cursorAdapter = new SimpleCursorAdapter(this, R.layout.list_row, cursor, columns, to, 0);
selectCategory.setAdapter(cursorAdapter);
}
}
}
这是堆栈跟踪
02-24 12:34:56.587 2610-2610/com.example.rajkumar.budgetanalysis E/AndroidRuntime﹕ FATAL EXCEPTION: main
Process: com.example.rajkumar.budgetanalysis, PID: 2610
java.lang.NullPointerException
at android.content.ContextWrapper.openOrCreateDatabase(ContextWrapper.java:256)
at android.database.sqlite.SQLiteOpenHelper.getDatabaseLocked(SQLiteOpenHelper.java:224)
at android.database.sqlite.SQLiteOpenHelper.getWritableDatabase(SQLiteOpenHelper.java:164)
at com.example.rajkumar.budgetanalysis.DBHelper.addAmount(DBHelper.java:95)
at com.example.rajkumar.budgetanalysis.AddListItems.addTotal(AddListItems.java:48)
at com.example.rajkumar.budgetanalysis.Quick.add(Quick.java:59)
at com.example.rajkumar.budgetanalysis.Quick$1.onItemLongClick(Quick.java:36)
at android.widget.AbsListView.performLongPress(AbsListView.java:2979)
at android.widget.AbsListView$CheckForLongPress.run(AbsListView.java:2928)
at android.os.Handler.handleCallback(Handler.java:733)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:136)
at android.app.ActivityThread.main(ActivityThread.java:5001)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:785)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:601)
at dalvik.system.NativeStart.main(Native Method)
DBHelper类:
package com.example.rajkumar.budgetanalysis;
import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
public class DBHelper extends SQLiteOpenHelper {
SeparateBudget data;
public static final String TABLE_NAME="my_budget";
public static final String ID="id";
public static final String OWNER_NAME="owner_name";
public static final String BUDGET_NAME="budget_name";
public static final String BUDGET_LIMIT="budget_limit";
static final String DB_NAME="TABLE_BUDGET.db";
static final int DB_VERSION=1;
public static final String KEY_ROWID="_id";
public static final String KEY_DATE="_date";
public static final String KEY_CATEGORY="list_cat";
public static final String KEY_AMOUNT="list_amount";
public static final String TABLE2_NAME = "my_expenses";
public static final String USER_NAME="user_name";
public static final String PASSWORD="password";
public static final String TABLE_LOGIN="login_table";
public static final String CREATE_TABLE="create table " + TABLE_NAME + " ( " + ID +" INTEGER "+
" PRIMARY KEY AUTOINCREMENT NOT NULL , " + OWNER_NAME + " TEXT ,"+ BUDGET_NAME +" TEXT ,"
+ BUDGET_LIMIT + " VARCHAR );";
public static final String CREATE_TABLE2="create table " + TABLE2_NAME + " ( " +
KEY_ROWID +" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL , " + KEY_CATEGORY + " TEXT UNIQUE ," +
KEY_AMOUNT + " VARCHAR );";
public static final String CREATE_LOGIN_TABLE="create table "+TABLE_LOGIN+" ( "+USER_NAME+" VARCHAR , "+
PASSWORD+" VARCHAR );";
public DBHelper(Context context){
super(context, DB_NAME, null, DB_VERSION);
}
public void onCreate(SQLiteDatabase db){
db.execSQL(CREATE_TABLE);
db.execSQL(CREATE_TABLE2);
db.execSQL(CREATE_LOGIN_TABLE);
Log.d("database handler", "Database created");
}
public void onUpgrade(SQLiteDatabase db,int oldVersion,int newVersion){
db.execSQL("DROP TABLE IF EXISTS " + TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + TABLE2_NAME);
onCreate(db);
}
public void addData(SeparateBudget separateBudget){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(OWNER_NAME,separateBudget.get_owner());
values.put(BUDGET_NAME,separateBudget.get_budget_name());
values.put(BUDGET_LIMIT, separateBudget.get_budget_limit());
db.insert(TABLE_NAME, null, values);
db.close();
}
public void createCategory(String list_cat,String list_amt){
SQLiteDatabase db=this.getWritableDatabase();
ContentValues values=new ContentValues();
values.put(KEY_CATEGORY,list_cat);
values.put(KEY_AMOUNT,list_amt);
db.insert(TABLE2_NAME,null,values);
db.close();
}
public Cursor getListItems(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor;
cursor=db.query(TABLE2_NAME,new String[]{KEY_ROWID,KEY_CATEGORY,KEY_AMOUNT},null,null,null,null,null);
if(cursor!=null){
cursor.moveToLast();
}
return cursor;
}
public SeparateBudget getData(){
SQLiteDatabase db=this.getReadableDatabase();
Cursor cursor=db.rawQuery(" SELECT * FROM "+TABLE_NAME,null);
if(cursor!=null && cursor.moveToLast()) {
data = new SeparateBudget(Integer.parseInt(cursor.getString(0)),
cursor.getString(1), cursor.getString(2),Integer.parseInt(cursor.getString(3)));
db.close();
cursor.close();
}
return data;
}
public void deleteData(SeparateBudget sep){
SQLiteDatabase db=getReadableDatabase();
db.delete(TABLE_NAME, ID + "=?", new String[]{String.valueOf(sep.get_id())});
}
public void deldata(long position){
SQLiteDatabase db=getReadableDatabase();
db.delete(TABLE2_NAME,KEY_ROWID + "=" +position,null );
}
public void addAmount(String amount,long id){
SQLiteDatabase db=getWritableDatabase();
db.execSQL("UPDATE "+TABLE2_NAME+" SET "+KEY_AMOUNT+"="+amount+", WHERE "+KEY_ROWID+" = "+id+" ;");
db.close();
}
}
答案 0 :(得分:0)
因此,在名为Quick的活动中,您将行3
更改为DBHelper dbHelper = new DBHelper(this);
然后,在onCreate中的setContentView方法之后添加DBHelper dbHelper;
它没有使用你的代码的原因是因为在调用onCreate之前不会创建Context。