这是我的活动,我需要在数据库中添加类别,我使用Android提示用户输入对话框添加该catgeory.I参考此链接的对话框。 http://www.mkyong.com/android/android-prompt-user-input-dialog-example/
但现在当我按下对话框中的ADD(OK)按钮时,它会给出空指针异常。
public class budget extends Activity {
int selected_id;
ListView rldlist = null;
DBhelper helper;
String budget;
TextView menubtn;
Context context = this;
EditText txr;
Button btn1;
SQLiteDatabase db;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.budget);
helper = new DBhelper(this);
txr = (EditText) findViewById(R.id.add);
btn1 = (Button) findViewById(R.id.button);
menubtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// get prompts.xml view
LayoutInflater li = LayoutInflater.from(context);
View promptsView = li.inflate(R.layout.addcategory, null);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set prompts.xml to alertdialog builder
alertDialogBuilder.setView(promptsView);
// set dialog message
alertDialogBuilder
.setCancelable(false)
.setPositiveButton("ADD",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
ContentValues value = new ContentValues();
value.put(DBhelper.Name, txr.getText().toString());
db = helper.getWritableDatabase();
if (helper.checkIdExist(txr.getText().toString())) {
db.insert(DBhelper.TABLE1, null, value);
db.close();
} else {
Toast.makeText(getApplicationContext(), "Duplicate Category Name!", Toast.LENGTH_LONG).show();
}
}
})
.setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
这是我的DB类..
public class DBhelper extends SQLiteOpenHelper{
static final String DATABASE = "wedding9.db";
static final int VERSION = 9;
static final String TABLE1 = "Category";
static final String TABLE2 = "Budget";
static final String TABLE3 = "Expenses";
static final String C_ID = "_id";
static final String Name = "name";
static final String B_ID = "_id";
static final String Description = "description";
static final String Amount = "amount";
public static final String ID1="_id";
public static final String DATE_T1="date1";
public static final String CATEGORY="category";
public static final String DETAIL="detail";
public static final String AMOUNT1="amount1";
public static final String STATUS="status";
public static final String EX_YEAR="exyear";
public static final String EX_MONTH="exmonth";
public DBhelper(Context context){
super(context, DATABASE, null, VERSION);
}
public void onCreate(SQLiteDatabase db){
db.execSQL("CREATE TABLE " + TABLE1+ "(" +C_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +Name+ " text unique not null)");
db.execSQL("CREATE TABLE " + TABLE2+ "(" +B_ID
+ " INTEGER PRIMARY KEY AUTOINCREMENT," +Description+ " text,"
+Amount+ " text, FOREIGN KEY ("+Description+") REFERENCES "+TABLE1+"("+Name+"));");
db.execSQL("CREATE TABLE " + TABLE3 + " ( "
+ ID1 + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ DATE_T1 + " text, "
+ CATEGORY + " text, "
+ DETAIL + " text, "
+ STATUS + " text, "
+ EX_YEAR + " text, "
+ EX_MONTH + " text, "
+ AMOUNT1 + " text, FOREIGN KEY ("+CATEGORY+") REFERENCES "+TABLE1+"("+Name+"));");
}
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
db.execSQL("drop table " + TABLE1);
onCreate(db);
}
public ArrayList<category> getCategories(){
ArrayList<category> arrayList = new ArrayList<category>();
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
while (c.moveToNext()) {
category cat = new category(c.getInt(0),c.getString(1));
arrayList.add(cat);
}
return arrayList;
}
public boolean checkIdExist(String name) {
SQLiteDatabase db = getReadableDatabase();
Cursor c = db.query(DBhelper.TABLE1, null, null, null, null, null, null);
while (c.moveToNext()) {
if(c.getString(1).equals(name))
return false;
}
return true;
}
}
10-25 13:56:21.179 16248-16248/com.example.username.weddingplanning
E/AndroidRuntime﹕ FATAL EXCEPTION: main
java.lang.NullPointerException
at com.example.username.weddingplanning.budget$1$2.onClick(budget.java:83)
at com.android.internal.app.AlertController$ButtonHandler.handleMessage(AlertController.java:185)
at android.os.Handler.dispatchMessage(Handler.java:99)
at android.os.Looper.loop(Looper.java:176)
at android.app.ActivityThread.main(ActivityThread.java:5299)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:511)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1102)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:869)
at dalvik.system.NativeStart.main(Native Method)
答案 0 :(得分:2)
下面:
Context context = this;
在创建可能导致问题的context
之前初始化Activity
。
在context
方法中初始化onCreate
:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.budget);
context=this;//<<<<
....
}
如果add
EditText位于addcategory
布局内,则使用提示视图初始化它,如onClick方法:
View promptsView = li.inflate(R.layout.addcategory, null);
txr = (EditText)promptsView. findViewById(R.id.add);
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);